0
我有一個JavaScript函數,它看起來是這樣的:使對象非全局中的JavaScript
foo=function(listenerObject)
{
a={count:0};
foo2=function(){
//does stuff with a
}
//register listenerObject to callback the function foo2
}
問題是,我最終會使用這個函數來註冊多個監聽器,但他們都最終共享相同的「一個對象。有沒有辦法讓函數內部的對象聲明進入全局範圍。 換句話說,我想阻止能夠做這樣的事
fun=function(){o={count:0}}
o.count++//o is now in the global scope