[英]What is the reason for the ratio of the function to a variable?
this not work这不起作用
function myCounter(){ let counter = 0; function plus(){ counter++; return counter; } return plus; } console.log(myCounter()); console.log(myCounter());
but this work但这项工作
function myCounter(){ let counter = 0; function plus(){ counter++; return counter; } return plus; } var add = myCounter(); console.log(add());
I know they are different in the syntax.我知道它们在语法上有所不同。 My main question is: Why function alone on the console.log does not work and should be attributed to a variable
我的主要问题是:为什么单独在 console.log 上的功能不起作用,应该归因于一个变量
Your function myCounter
only returns a function reference.您的函数
myCounter
只返回一个函数引用。 It does not call the function plus
.它不会调用函数
plus
。
In your first example you only call the function myCounter
:在您的第一个示例中,您只调用函数
myCounter
:
console.log(myCounter());
In your second example you first call the function myCounter
that returns a function reference:在第二个示例中,您首先调用返回函数引用的函数
myCounter
:
var add = myCounter();
and then you call the returned function:然后调用返回的函数:
console.log(add());
Solution:解决方案:
You have to change this line你必须改变这一行
return plus;
to到
return plus();
This works:这有效:
function myCounter(){ let counter = 0; function plus(){ counter++; return counter; } return plus(); } console.log(myCounter());
In the second example this line: var add = myCounter();
在第二个例子中这一行:
var add = myCounter();
makes the add
var to be only a reference to a function, if you will console log the add var without the brackets, it will print only [Function]
, but the console.log(add());
使
add
var 仅是对函数的引用,如果您将控制台记录不带括号的 add var,它将仅打印[Function]
,但console.log(add());
makes the add function to be invoked.使 add 函数被调用。
In order to make the first example to work, you can change the return statement of the myCounter
counter function.为了使第一个示例工作,您可以更改
myCounter
计数器函数的返回语句。
This code make the myCounter
to return only the plus
function reference:此代码使
myCounter
仅返回plus
函数引用:
function myCounter(){
function plus(){
//code
}
return plus;
}
So in order to make it work you should invoke the myCounter
twice:所以为了让它工作,你应该调用
myCounter
两次:
console.log(myCounter()());
But this make the plus
function to be invoked when the myCounter
is invoked (called) from inside the console.log(myCounter())
:但这使得在从
console.log(myCounter())
内部调用(调用) myCounter
时调用plus
函数:
function myCounter(){
function plus(){
//code
}
return plus();
}
You are missing the closure concept.你错过了闭包的概念。 Calling a
myCounter
function will return you another function and it will initialize "private" variable counter
inside, so myCounter() -> function
.调用
myCounter
函数将返回另一个函数,它将在内部初始化“私有”变量counter
,因此myCounter() -> function
。
Of course you can call this way myCounter()()
, but in this case the "private" counter
variable will be initialized every call with value 0, and won't be useful.当然,你可以这样调用
myCounter()()
,但在这种情况下,“私有” counter
变量将在每次调用时初始化为 0,并且没有用。
The solution is to store result of myCounter()
in variable and call it later, so you will have expected behaviour.解决方案是将
myCounter()
结果存储在变量中并稍后调用,因此您将获得预期的行为。
function myCounter(){ let counter = 0; function plus(){ counter++; return counter; } return plus; } console.log(myCounter()()); console.log(myCounter()()); console.log(myCounter()()); console.log('====') var add = myCounter(); console.log(add()); console.log(add()); console.log(add());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.