How is data passed to anonymous functions in JavaScript?
问题 When I pass 'this' to an anonymous function like so: MyClass.prototype.trigger = function(){ window.setTimeout(function(){this.onTimeout();},1000); } I get a "this.onTimeout is not a function"-error. I guess that 'this' is no longer available at the time the anonymous function is executing? So I've been doing this: MyClass.prototype.trigger = function(){ var me = this window.setTimeout(function(){me.onTimeout();},1000); } Is this really how you're supposed to do things? It kinda works, but it