为什么我得到值“结果”为这个闭合? [英] Why do I get the value "result" for this closure?
本文介绍了为什么我得到值“结果”为这个闭合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有这个代码(小提示)用于记忆模块:
Let's say I have this code (fiddle) intended to memoize modules:
var chat = {
// Create this closure to contain the cached modules
module: function() {
// Internal module cache.
var modules = {};
console.log('in module:', name); // <---------- "in return: result"
// Create a new module reference scaffold or load an
// existing module.
return function(name) {
console.log('in return:', name); // <---------- "in return: derp"
// If this module has already been created, return it.
if (modules[name]) {
return modules[name];
}
// Create a module and save it under this name
return modules[name] = { Views: {} };
};
}()
};
chat.module("derp");
代码中没有提到的result这个短语。为什么会在第一个控制台日志中返回该值?
Nowhere in the code is the phrase "result" even mentioned. Why would it return that value in the first console log?
此外,如果 name
参数中没有指定 c $ c> module:function(){}
声明?
Also, how does the return function get the value for the name
parameter when none is specified in the module: function() {}
declaration?
推荐答案
console.log('in module:', name);
在您声明之前,您正在记录 name
它。因此,它使用 window.name
。
You are logging name
before you ever declare it. So it's using window.name
instead.
这篇关于为什么我得到值“结果”为这个闭合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文