我有我的對象這樣的功能:爲什麼這不被認爲是一種功能?
var time = {
warps : 3,
warpCounter : 50,
warp : function(){
if (this.warps > 0){
this.warps--;
this.warpLoop = 50;
this.warpLoop();
}
},
warpLoop : function(){
setTimeout(function() {
this.increment();
if (warpCounter--){
this.warpLoop();
}else{
if(this.warps > 0){
htmlInteraction.enableButton('warp-button');
}
}
}, 100);
},
};
當我嘗試從另一個方法調用它(使用this.warpLoop()
),我得到:
Uncaught TypeError: Property 'warpLoop' of object #<Object> is not a function
這是爲什麼?
@dystroy我已經添加了整個對象。 – fredley
您需要閱讀關於'this'如何在Javascript中工作的內容。直到你理解它纔會相當混亂(如果你認爲你理解它,甚至會更加困惑!)。網絡上有很多和很多資源,這是一個非常普遍的問題。 [這將是一個很好的起點](http://stackoverflow.com/questions/3127429/javascript-this-keyword),但如果你搜索它還有很多。 – Spudley