var foo = (function() {
var proxy = {},
warning = false;
proxy.warn = function(msg) {
if (!warning) {
warning = true;
alert(msg);
}
return this; //For the purpose of method chaining we return proxy object.
}
function func() {
alert(warning); //This is a private function relative to foo.
}
return proxy;
}());
foo.warn(); //will alert
foo.warn(); //will not alert since warning has been set to true
我对这里的实例化感到困惑,因为没有使用新关键字谁持有警告值?就范围警告而言,这里是否有任何泄漏。
谢谢你。