可以在javascript中创建全局范围闭包吗?

时间:2012-11-17 08:58:13

标签: javascript scope global-variables

目标是允许全局变量(例如currentAccount)根据执行上下文同时引用多个值。

var context = new Context();
context.currentAccount = "mine";
context.execute(function() {
   // any code in or called by this function 
   // needs access to the `currentAccount` variable

   // for example
   var model = new Model();
   model.doSomething(); // model needs to be able to refer to `currentAccount`
});

context = new Context();
context.currentAccount = "yours";
context.execute(function() {
   ...
   model.doSomething(); // `currentAccount` => "yours"
});

0 个答案:

没有答案