Why brackets are ignored in the expression (o.method) ()

I want to understand why the bracket around o.methodin the expression

(o.method)()

are ignored and therefore behave identically to o.method(), with the execution context methodreferencing o. I expected it to behave similarly (o.method || true)(), where the execution context inside methodrefers to a global object.

If I evaluate (o.method)it myself , it returns a link to an autonomous function that is not related to any context. Just rewriting it like this:

var a = (o.method); a(); 

will have a global context as expected. And I just shortened the code by replacing a, and it produced a different result.

+4
source share
2 answers

, ES5 11.1.6:

11.1.6 # Ⓣ

PrimaryExpression : ( Expression ) :

  • Expression. Reference.

GetValue . , , delete typeof, .

, JS:

, o.method ( ):

- . , , .

, o.method YET; [[o, "method", false]]. , o.method(), GetValue, ( 11.2.3).

(o.method)(), (o.method) - - GetValue - ( 11.1.6). .

(o.method || true)(), || GetValue ( 11.11) - . , , ( ) .

+8

Invoice . , . , . (.. a. Dot [subscript]), .

, , o.method, 'o'.

.

, : https://www.safaribooksonline.com/library/view/javascript-the-good/9780596517748/ch04s03.html

-3

Source: https://habr.com/ru/post/1659482/


All Articles