所以,我想要做這樣的事情:進樣可變進功能的範圍
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'變量。
那麼讓它成爲全球? – 2013-03-21 19:15:35
這樣做的應用程序是什麼?有可能是其他技術人們可能會建議 – 2013-03-21 19:29:51
也許這是一個(複雜)解決方法:http://stackoverflow.com/questions/10060857/is-it-possible-to-achieve-dynamic-scoping-in-javascript-without- evaing-to-eva – bfavaretto 2013-03-21 19:30:27