News for hackers. Stuff that matters.
Re:“This” in JavaScript by zenojevski on Monday December 08, 2014 @04:26AM (#8715631)
The way you've written this code, `this` refers to `o`, as it has been evaluated at the time of execution of `f()` (first case) rather than inside the `setTimeout` callback, at which point it'd be `Window` (second case). So yes.

Basically your code is doing this right now:

    o.f = function(){
        console.log(this); // logs `o`, returns `undefined`
        setTimeout(1000, undefined);
    });
If you did this:

    o.f = function(){
        setTimeout(1000, function(){
            console.log(this);
        });
    };
Then it'd be `Window` as in rule #2.

There are no comments on this comment yet.