简体   繁体   English

为什么不能将其识别为功能?

[英]Why isn't this recognized as a function?

I have this function in my object: 我的对象中有此功能:

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);
    },

};

When I try and call it from another method (using this.warpLoop() ) I get: 当我尝试从另一个方法(使用this.warpLoop() )调用它时,我得到:

Uncaught TypeError: Property 'warpLoop' of object #<Object> is not a function 

Why is this? 为什么是这样?

this context in setTimeout changes, you can use closures to keep the this context. 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();

your code can look like 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();

The this value in JavaScript is not lexically defined. JavaScript中的this值未按词法定义。 It's defined by the manner in which the function was invoked. 它是通过调用函数的方式定义的。

A typical fix is to store the value of this in a variable in the enclosing scope, then reference it in the inner scope. 一个典型的解决方法是,它的值存储this在封闭范围的变量,然后在内部范围引用它。

var that = this;

setTimeout(function() {
    that.whatever()
}, 1000)

While you could also bind the outer this value to your callback using Function.prototype.bind() , you seem to have an .increment() method that isn't throwing the error. 虽然您也可以使用Function.prototype.bind()将外部this值绑定到您的回调中,但是您似乎拥有一个.increment()错误的.increment()方法。 So binding may break that. 因此,绑定可能会破坏这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM