對不起,如果這已被回答,但我無法找到它...一個困難的事情來搜索,我想!在JavaScript(和jQuery.extend)中確定「this」
說我有這樣的:
var MyPrototype = function() { this.init(); }
$.extend(MyPrototype.prototype, {
a: 5,
init: function() {
var thing = new SomeOtherClass();
thing.meth = this.meth;
// thing.meth() is called somewhere within thing;
},
meth: function() {
alert(this.a);
}
}
基本上,我處理的,它使用自己的方法爲回調,如其他類我希望用我自己的功能覆蓋它們。但我在做這件事的時候需要保留this
的適當範圍(我關心的SomeOtherClass
唯一關心的是傳遞給回調的東西;狀態中沒有任何東西)。
如你所想,這不起作用,因爲thing
沒有a
屬性!我並不熟悉JavaScript的複雜性範圍,知道如何使this
參考我想要的東西,不過!
請參閱:http://stackoverflow.com/questions/520019/controlling-the-value-of-這在一個jQuery的事件...還有:http://stackoverflow.com/questions/1043556/how-can-i-keep-the-context-of-this-in-jquery ...和真的,大多數問題在:http://stackoverflow.com/questions/tagged/this+javascript(這是一個非常常見的混淆點) – Shog9 2009-07-27 22:44:40