Javascript 为什么不是';这难道不被认为是一种功能吗?
我的对象中有此函数:Javascript 为什么不是';这难道不被认为是一种功能吗?,javascript,Javascript,我的对象中有此函数: var time = { warps : 3, warpCounter : 50, warp : function(){ if (this.warps > 0){ this.warps--; this.warpLoop = 50; this.warpLoop(); } }, warpLoop : function(){
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:对象#的属性“warpLoop”不是函数
这是为什么?JavaScript中的
这个值没有词汇定义。它由调用函数的方式定义
典型的修复方法是将this
的值存储在封闭范围内的变量中,然后在内部范围内引用它
var that = this;
setTimeout(function() {
that.whatever()
}, 1000)
虽然您也可以使用函数.prototype.bind()
将外部此
值绑定到回调,但您似乎有一个.increment()
方法没有抛出错误。因此绑定可能会破坏这一点。此上下文在setTimeout更改中,您可以使用闭包来保持此上下文
var test={
warpLoop : function(){
var me=this;//set closure to get the right this context
setTimeout(function () {
console.log("this is:",this); // is window
console.log("me is:",me); // is test
// can call me.warpLoop() but not this.warpLoop()
}, 100);
}
}
test.warpLoop();
您的代码可以如下所示:
var time = {
warps : 3,
warpCounter : 3,
warp : function(){
if (this.warps > 0){
this.warps--;
this.warpLoop = 50;
this.warpLoop();
}
},
warpLoop : function(){
//the setTimeout calls me.warpCounter not this.warpCounter
// wich is the same as window.warpCounter since the next
// line is not part of the setTimeout execution you can
// use this
console.log("warpLoop called,warpCounter is",this.warpCounter);
var me=this;
setTimeout(function () {
//me.increment();
if (me.warpCounter--){
me.warpLoop();
}else{
if(me.warps > 0){
//htmlInteraction.enableButton('warp-button');
}
}
}, 100);
},
};
time.warpLoop();
@我已经添加了整个对象。您需要了解这个在Javascript中是如何工作的。在你理解它之前,它可能会非常混乱(如果你认为你理解它,甚至会更加混乱!)。网上有很多关于这方面的资源;这是一个非常普遍的问题,但是如果你搜索它,还有很多。我尝试将warpLoop
更改为self
的参数,并将其传递给python样式,但这没有帮助…@TomMedley:不,除非定义一个self
变量来引用对象,否则没有帮助。基本上,如果希望warpLoop
中this
的值引用特定对象,则应该从该对象调用warpLoop
,如my_object.warpLoop()
中所述。这就是为什么我们在变量中保留外部这个值。有关闭包的信息,请查看MDN。有些奇怪的事情正在发生,我可以从对象外部调用warpLoop
,但不能从对象内部的另一个方法调用(我得到了问题中的错误)。这是为什么?@TomMedley我已经包含了针对您情况的代码。注释掉了您尚未发布的函数,因为这将生成错误。@TomMedley因为您只有一个“time”实例,所以您也可以使用time.warpLoop访问属性。如果您计划创建多个实例,我会坚持使用this关键字并在闭包中保留“this”值,这样您就可以在超时、ajax、事件处理程序回调或任何更改“this”上下文的异步进程中使用它
var time = {
warps : 3,
warpCounter : 3,
warp : function(){
if (this.warps > 0){
this.warps--;
this.warpLoop = 50;
this.warpLoop();
}
},
warpLoop : function(){
//the setTimeout calls me.warpCounter not this.warpCounter
// wich is the same as window.warpCounter since the next
// line is not part of the setTimeout execution you can
// use this
console.log("warpLoop called,warpCounter is",this.warpCounter);
var me=this;
setTimeout(function () {
//me.increment();
if (me.warpCounter--){
me.warpLoop();
}else{
if(me.warps > 0){
//htmlInteraction.enableButton('warp-button');
}
}
}, 100);
},
};
time.warpLoop();