How is data passed to anonymous functions in JavaScript?

后端 未结 3 1700
情话喂你
情话喂你 2021-02-10 02:42

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:25

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

提交回复
热议问题