Demystify parentheses around literal

javascript

Solution

`3.` is parsed as a decimal number (as in `3.0`). To use the `.` to access a member, you need to prevent it from parsing as part of the number literal.

Adding a space before the `.`, or a second `.`, would also help.

Problem

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 changed to `3.add(4)`; otherwise, returns 7. DEMO: http://jsfiddle.net/smacky311/m3NwK/2/ Why exactly does this happen? I read that parentheses around JSON can be used to convert the JSON to an object literal. However, the way the process was described the expression was interpreted as an object literal because of the initial `{` which does not apply in this case. Under what condition(s) does the interpreter determine that a literal is an expression? Anytime we add parenthesis?

Original source