【发布时间】:2012-04-09 22:22:26
【问题描述】:
我计划在正在构建的框架中做“扩展点”,我正在寻找一种方法,我可以如何为扩展提供“核心”,以便它可以添加功能但不暴露可以任意的核心对象被操纵(我知道提供一个界面是更好的主意)但是,当我在测试(以及学习)时,我想知道为什么会发生这种情况:
(function() {
var core = {'bar': 'foo'}
function getCore() {return core;}
window.kit = {
core: core,
getCore: getCore
}
}());
//initial check
console.log(kit.core)
console.log(kit.getCore());
//change the bar
kit.core.bar = 'baz';
//we will both see 'baz'
console.log(kit.core)
console.log(kit.getCore());
//null the core
kit.core = null;
//this time...
console.log(kit.core) //core is null on this one
console.log(kit.getCore()); //why is the core still there on this one?
【问题讨论】:
标签: javascript closures