JavaScript object functions and `this` when unbound and returned in expression/parens

后端 未结 2 1177
予麋鹿
予麋鹿 2020-11-29 13:18

Lines 1-2 and 4-5 make sense in terms of the this returned. What am I missing about line 3? I thought it would return window similar to lines 4-5.

2条回答
  •  孤城傲影
    2020-11-29 13:44

    The grouping operator does not destroy property references, which are provoking the method call.

    This is explicitly mentioned in the spec:

    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.

    In your lines 4 and 5, it's not the parenthesis but the operators (?: and ||) that de-reference the property and yield the "unbound" function.

提交回复
热议问题