简体   繁体   English

如何用闭包调用Javascript函数?

[英]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: 问题:

  1. Why in version one, when you use var say2 to call sayHello2, you don't have to add another set of empty parentheses? 为什么在版本1中,当您使用var say2调用sayHello2时,不必添加另一组空括号?
  2. If I take off the line return say;, the error message will say sayHello2 is not a function, why? 如果我下线返回say ;,错误消息将说sayHello2不是函数,为什么?
  3. Why is the point of say2(); 为什么说say2()的要点; (the last line in version2)? (版本2的最后一行)? If I delete that line, nothing changes, 'HelloDavid' will still be logged. 如果删除该行,则没有任何变化,“ HelloDavid”仍将被记录。
  1. 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()的返回值来调用该返回的函数。

  2. If there's no return statement, then sayHello2() returns undefined . 如果没有return语句,则sayHello2()返回undefined

  3. 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版,我们的somethingsayHello2("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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM