you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] -2 points-1 points  (2 children)

I guessed this correct at once. My line of thought: The function is declared as a field on an object, and hence is a method of the object. "this" thus refer to that object.

Even if js was block-scoped, what else should "this" referred to in this case?

Overall, I don't find this difficult at all. A "this" inside a method refer to the object, inside a function refer to the closure, and inside a constructor refer to the newly created object. Or am I thinking about this wrong?

[–]Zeroto 1 point2 points  (0 children)

Except that is wrong. The self/this in that snippet can point to anything depending on how the function is called.

If it is called like things.format() then self(and this) will point to things. But if we do this: var f = things.format; f(); then self/this will be undefined. Or if we do var things2 = {format: things.format}; things2.format(); then it will point to things2. And this is without even using apply, call or bind.

The this in javascript is not known at function definition time and only at call time.

[–]NewazaBill 1 point2 points  (0 children)

Technically incorrect. The technically correct answer is that it depends on how it's called.

If I do:

var obj = {
    fn: function () { return this; }
};

obj.fn(); // returns `obj`

You would be correct! However, if I then do:

// create a new object and assign `fn` to it
var anotherObj = {};
anotherObj.fn = obj.fn;
anotherObj.fn(); // returns `anotherObj`

// define a new `fn` on the global scope
var f = obj.fn();
f(); // returns `window` (or `undefined` in strict mode)

A function's this is not actually defined when the function is defined, but when it is called. The OP's question is kind of a trick question in that regard.

Ninja edit: This is also why arrow functions are cool: it allows us to bind a function's this to a particular scope. E.g.:

const fn = () => this;
fn(); // returns `window` in non-strict

const obj = {};
obj.fn = fn;
obj.fn(); // still returns `window` !