Demystify parentheses around a literal

Given:

Number.prototype.add = methodize(add);

function methodize(func) {//a function that converts a binary function to a method
    return function (x) {        
            //console.log(x);            
            console.log(this);
            return func(x,this);            
        }
}

function add(x, y) {
    return x + y;
}

console.log((3).add(4));

The final line (3).add(4)throws an exception if it is changed to 3.add(4); otherwise returns 7.

DEMO: http://jsfiddle.net/smacky311/m3NwK/2/

Why is this exactly happening? I read that parentheses around JSON can be used to convert JSON to an object literal. However, as the process was described, the expression was interpreted as an object literal because of the initial one {, which is not applied in this case.

Under what conditions does the interpreter determine that a literal is an expression? Anytime we add brackets?

+4
source share
1 answer

3. ( 3.0).
. , .

. . .

+6

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


All Articles