Hacker News new | ask | show | jobs
by zenojevski 4209 days ago
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.