Search code examples
javascriptiife

why 'this' point to 'window' obj when using assignment operator in iife?


I was wondering why the example will return 'global' not 'obj2'? And what's different between '(obj2.say = obj1.say)()' and '(obj2.say)()'? Here is the code:

var text = 'global';
var obj1 = { text: 'obj1', say: function () {console.log(this.text)}};
var obj2 = { text: 'obj2'};
(obj2.say = obj1.say)();


Solution

  • The result of an assignment is the value that was assigned. Example:

    var foo, bar;
    foo = (bar = 42);
    console.log(foo); // 42
    

    Hence when you do (obj2.say = obj1.say), the result of the assignment, was returned by the grouping operator, is the function object in obj1.say.

    The whole expression to be be equivalent to

    var result = obj2.say = obj1.say;
    result();
    

    And when a function is called the "normal" way (func()), this refers to the global object or is undefined in strict mode.


    (obj2.say)() is quite special actually. The grouping operator alone does not resolve (internal) references to values. I.e. the result of obj2.say is, internally, a reference that describes the member access say on obj2. The grouping operator, (...) returns that reference unchanged, not resolving it to the actual function object. That's why this will correctly point to obj2. Omitting the grouping operator has the same effect.

    This is actually called out in the spec:

    This algorithm does not apply GetValue to the result of evaluating Expression. The principal motivation for this is so that operators such as delete and typeof may be applied to parenthesized expressions.