[英]How to call a Javascript function with closure?
Version 1 版本1
function sayHello2(name){ var text='Hello'+name; var say=function(){ console.log(text); }; return say; } var say2=sayHello2("David"); say2();
Version 2 版本2
function sayHello2(name){ var text='Hello'+name; var say=function(){ console.log(text); }; return say; } sayHello2("David")();
Questions: 问题:
In both samples of code, two function calls are made. 在这两个代码示例中,进行了两个函数调用。 The difference between the first and second is that in the first sample, the result of the first function call is stored in
say2
before the second function call is made. 第一个和第二个之间的区别在于,在第一个样本中,在进行第二个函数调用之前,将第一个函数调用的结果存储在
say2
。 The second sample immediately uses the return value from sayHello2()
to call that returned function. 第二个示例立即使用
sayHello2()
的返回值来调用该返回的函数。
If there's no return
statement, then sayHello2()
returns undefined
. 如果没有
return
语句,则sayHello2()
返回undefined
。
The claim of this question, that removing the say2();
这个问题的主张,即删除
say2();
line does not change the behavior, is incorrect. 行没有改变行为,是不正确的。 If you don't call
say2()
then nothing will be logged. 如果不调用
say2()
则不会记录任何内容。
Look at sayHello2()
: 看看
sayHello2()
:
function sayHello2(name){
// Initialize the text that will be logged
var text='Hello'+name;
// Create the function that will log the message
var say=function(){
console.log(text);
};
// Return the logging function
return say;
}
The sayHello2()
function returns a reference to that little function that calls console.log()
. sayHello2()
函数返回对该调用console.log()
小函数的引用。 Because functions are object values in JavaScript, a reference to a function works as well as any other sort of value, and can be stored in a variable or used in an expression. 由于函数是JavaScript中的对象值,因此对函数的引用与任何其他类型的值一样有效,并且可以存储在变量中或在表达式中使用。
Note that when the logging function is created, that's done with a var
initailization: 请注意,在创建日志记录功能时,这是通过
var
initailization完成的:
var say=function(){
console.log(text);
};
That's essentially the same thing that happens to the variable say2
: 本质上,变量
say2
发生的事情是相同的:
var say2=sayHello2("David");
except that here, on the right-hand side of the =
sign there's a function call instead of a function instantiation. 除了在这里,在
=
号的右侧有一个函数调用而不是函数实例化。
The statement 该声明
say2();
therefore is calling that little logging function that was returned from the call to sayHello2()
. 因此正在调用从调用
sayHello2()
返回的那个小的日志记录函数。
The second code sample does the same thing: 第二个代码示例执行相同的操作:
sayHello2("David")();
except that there's no intermediate storage of the returned function reference. 除了没有中间存储返回的函数引用。 Instead, the reference is immediately used to make the second function call.
而是,该引用立即用于进行第二个函数调用。
In the first version, count the function calls made: 在第一个版本中,计算进行的函数调用:
var say2 = sayHello2("David"); // First function call
say2(); // Second function call
In the second version: 在第二个版本中:
// First // Second
sayHello2("David")();
In JavaScript, an expression of the form 在JavaScript中,形式为
something()
always means, "Interpret something
as a reference to a function, and call it (passing no parameters)." 始终表示“将
something
解释为对函数的引用,然后调用它(不传递任何参数)”。 If something
is not a reference to a function, you get an exception. 如果
something
内容不是对函数的引用,则会出现异常。 So in version 2, our something
is sayHello2("David")
. 所以在第2版,我们的
something
是sayHello2("David")
Because that's got the second set of empty parentheses after it, it means we're telling JavaScript to make another function call. 因为那是紧随其后的第二组空括号,所以这意味着我们要告诉JavaScript进行另一个函数调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.