【问题标题】:How does closure return work? [duplicate]关闭返回如何工作? [复制]
【发布时间】:2015-07-29 20:46:59
【问题描述】:

我有两个简单闭包的例子,我的问题是我什么时候应该返回带或不带括号的函数?谢谢!

示例 1:

function names (first, last) {
    var intro = "My name is ";
    function full_Name () {
        return intro + first + " " + last;
    }
    return full_Name();   <----
}

full_Name("Macro","phages"); // My name is Macro phages

示例 2:

function names (first) {
    var intro = "My name is ";
    function full_Name (last) {
        return intro + first + " " + last;
    }
    return full_Name;    <---- why not full_Name();
}

var white_cells = names("Macro");
white_cells("phages");  // My name is Macro phages

【问题讨论】:

    标签: javascript


    【解决方案1】:

    return foo(); 调用函数并返回其返回值(在您的示例中为intro + first + " " + last;)。

    return foo; 返回函数本身。

    【讨论】:

      猜你喜欢
      • 2014-12-11
      • 2014-12-04
      • 2010-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-10
      • 1970-01-01
      相关资源
      最近更新 更多