Why do I get the value “result” for this closure?

女生的网名这么多〃 提交于 2019-11-29 12:07:23
console.log('in module:', name);

You are logging name before you ever declare it. So it's using window.name instead.

… = function() {
    // …
    console.log('in module:', name);  // <---------- "in return: result"     
    return function(name) {…};
}();

You see, in the scope of that immediately-executed anonymous function expression, there is no variable "name". Therefore, the global variable window.name will be used - it's value seems to be "result" in your case (the jsfiddle target iframe's name) - try the unwrapped page, it will log an empty string.

Just for reference... "result" in this example, is the name (or better say the ID) of the lower right window (div/iframe) in JsFiddle...

So as @Rocket & @Bergi already says... you are getting the "window.name"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!