为什么当我使用参数调用函数时输出不同,并且在Javascript中通过()调用函数? [英] Why the output are different when I use the parameter to call the function, and invoke the function by() in Javascript?

查看:133
本文介绍了为什么当我使用参数调用函数时输出不同,并且在Javascript中通过()调用函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  function a(){
console.log('A!');
函数b(){
console.log('B!');
}
return b;
}

当我这样做时,

  var s = a(); 

以下是输出:

  A! 

当我这样做时,

  a(); 

输出如下:

  A! 
ƒb(){
console.log('B!');
}

我想知道为什么输出不同。因为函数 a 返回一个函数 > b



因此,这样做 console.log(a())将打印函数 b 的源代码。



如果要执行返回的函数,只需调用它:

function a(){console.log('A!');函数b(){console.log('B!'); } return b;} var s = a()console.log(s); console.log(--------------------------- -----------)s();


 function a() {
     console.log('A!');
     function b(){
         console.log('B!'); 
     }
     return b;
 }

When I do like this,

 var s = a();

Here is the output:

 A!

When I do like this,

 a();

The output is as below:

 A!
 ƒ b(){
     console.log('B!'); 
 }

I wonder why there the outputs are different.

解决方案

Because the function a returns a function b.

So, doing this console.log(a()) will print the source code of function b.

If you want to execute the returned function, just call it:

function a() {
  console.log('A!');

  function b() {
    console.log('B!');
  }

  return b;
}

var s = a()
console.log(s);
console.log("--------------------------------------")
s();

这篇关于为什么当我使用参数调用函数时输出不同,并且在Javascript中通过()调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆