Given:
Number.prototype.add = methodize(add);
function methodize(func) {
return function (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?
source
share