why do parenthesis preserve this when called in same statement

Viewed 36

Consider the following variables:

var obj = {
    value    : 'from object',
    getValue : function() { return this.value; }
};

var value = 'from global';

Now, obj.getValue() evaluates to 'from object'. And if I get a reference to just the getValue function and call it:

var f = obj.getValue;
f();

f evaluates to 'from global'.

My question is why does (obj.getValue)(); return 'from object'?

I would have thought that the first set of parenthesis would evaluate to a plain reference to the getValue function and then when calling that result, this would be the global context. Why does the interpreter assume this is a call on the object?

1 Answers
Related