是否有人可以解释如下的代码?我感到困惑的 return f();
关闭和返回值
作用是否
return f();
调用方法f
并返回结果? (即返回f
)return f();
是否返回一个对象?
如果我改变return f();
只是f();
然后checkscope = undefined
var scope = "global scope"; // A global variable
function checkscope() {
var scope = "local scope"; // A local variable
function f() { return scope; } // Return the value in scope here
return f();
}
checkscope() // => "local scope"
1)是2)否,它返回一个字符串3)如果你删除'return',那么'checkscope'不会返回任何东西,因此你会得到'undefined'。 – vlaz
可能的重复[JavaScript关闭如何工作?](http://stackoverflow.com/questions/111102/how-do-javascript-closures-work) – vlaz
F返回到它所说的。这里的重点是显示ES5范围,例如,只有函数范围。 –