Javascript Passing a method of an object as a function

PagEtept

New Member
Today I found out a rather strange behaviour that happens when you pass the method of an object as a function in Javascript.\[code\] setTimeout(myObject.test, 100); // "this" is the global object window\[/code\]The method "test" is correctly invoked, but "this" is not the object "myObject" but instead the global object "window". I can get the expected behaviour when I do something like this:\[code\]setTimeout(function(){myObject.test()}, 100); // "this" is myObject\[/code\]This seems to me rather strange. Can anybody explain, why this is.
 
Back
Top