Take a look at this ...
var a = { b: function() { console.log(this); } }
jsFiddle .
Assuming the expected result is what I expected ...
Example 1
When b() called, the Object property, this becomes the Object property, here it is the parent of a . It gives the expected result.
Example 2
eval() intended to take its execution context where it is called, in this case window . It also gives the expected result.
Example 3
When passing a string to setTimeout() , I would suggest that it runs something very similar to eval() . It also gives the expected result.
Example 4
this becomes window in this example. It interests me.
Example 5
Here, this becomes window , because c parent of the window .
When passing only a reference to a function (for example, ab ), will this always be window when called with () ?
The only way to save it this as a , to pass it as a string in setTimeout() / setInterval() ?
Thanks.
source share