How is data passed to anonymous functions in JavaScript?

后端 未结 3 2119
萌比男神i
萌比男神i 2021-02-10 03:01

When I pass \'this\' to an anonymous function like so:

MyClass.prototype.trigger = function(){
    window.setTimeout(function(){this.onTimeout();},1000);
}
         


        
3条回答
  •  一整个雨季
    2021-02-10 03:44

    you will have the same problem if inside your new method: newRequest you have to use a "for" or a "while" statement. Another solution could be creating a closure:

    like that:

    $.getJSON("answer.html",{},(function(me){return function(data){me.gotAnswer(count);}})(this));
    

提交回复
热议问题