我使用的显示模块模式有点像这样:
var SomeClosure = function () {
var SomeInnerVar = 4;
function InnerGetTheVar() {
return SomeInnerVar;
}
function InitClosure() {
// there's something else happening here that requires me to
// wait for document.ready before initializing the closure
setInterval(IncreaseSomeInnerVar, 10000);
}
function IncreaseSomeInnerVar() {
SomeInnerVar = SomeInnerVar + 1;
}
InitClosure();
return {
GetTheVar: InnerGetTheVar
}
}
$(document).ready(function () {
SomeClosure = SomeClosure();
});
现在为什么如果在控制台中输入以下内容:
SomeClosure.GetTheVar = 2;
SomeClosure.GetTheVar; // outputs 2!
为什么关闭不保持SomeInnerVar
保护?我不希望外部范围能够像那样更改 SomeInnerVar。
感谢您的建议。