我有两个简单闭包的例子,我的问题是我什么时候应该返回带或不带括号的函数?谢谢!
示例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
答案 0 :(得分:1)
return foo();
调用函数并返回其返回值(在您的示例中为intro + first + " " + last;
)。
return foo;
返回函数本身。