Nature of JS bound functions and function invocation operator

前端 未结 3 1972
遇见更好的自我
遇见更好的自我 2020-12-11 08:17
var obj = {};

var r1 = (obj[\'toString\'])();
var m1 = obj[\'toString\'];
var r2 = m1();

var r3 = (obj.toString)();
var m2 = obj.toString;
var r4 = m2();
         


        
3条回答
  •  悲哀的现实
    2020-12-11 08:49

    This is actually a "special" behavior of the grouping operator (...):

    1. Return the result of evaluating Expression. This may be of type Reference.

    NOTE 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 parenthesised expressions.

    So, this operator specifically does not call GetValue and thus does not return the function object itself but rather the whole reference, so that operations which expect a reference still work.


    A Reference is basically an encapsulation of a value with an optional "base value" which is the object in case of a property access.

提交回复
热议问题