【发布时间】:2013-03-21 19:14:48
【问题描述】:
所以,我想做这样的事情:
var a = 'a';
var dummy = function() {
// Print out var 'a', from the scope above
console.log('Dummy a: ' + a);
// Print out 'b', from the 'compelled' scope
console.log('Dummy b: ' + b);
}
(function() {
var b = 'otherscope';
// I know apply won't work, I also don't want to merge scopes
dummy.apply(this);
// I want something like this:
dummy.compel(this, [], {b: 'injected!'});
})();
但这行不通。
我实际上并不希望函数能够达到 2 个作用域,我希望能够从外部设置虚拟函数内部使用的 'b' 变量。
【问题讨论】:
-
执行此操作的应用程序是什么?人们可能会建议其他技术
-
也许这是一个(复杂的)解决方法:stackoverflow.com/questions/10060857/…
-
我主要是想看看有没有可能。好吧,我实际上想在 node.js MVC 中使用它。我不喜欢每个动作都设置参数,这样会更好。 @bfavaretto 向我展示了一个不错的解决方法,尽管我担心使用 eval() 会对性能造成太大影响:)
标签: javascript node.js