What exactly is the parsing rule in JS that results in the following:
Let's say we have this function
getThis = function(){
return this;
}
These all work as expected using the "previous dot" rule:
getThis(); //=> Window
obj = {getThis: getThis};
obj.getThis(); //=> obj
getThisTwo = obj.getThis;
getThisTwo(); //=> Window
However, this surprises me:
(obj.getThis)() //=> obj ...WAT
My intuition would be that it would behave exactly like the 3rd example (getThisTwo
). Ie, the part in parentheses is evaluated, which returns an anonymous function, which is then invoked. My expectation then is that this
would be Window
, not obj
.
Is this a special case, or is my understanding of how this
is resolved faulty?
(Edited to make the reason for my confusion clearer)