I like the idea. We already have something pretty similar. See the es6features GitHub repository. However, it is only for object and class initializers.
// ES5:
var obj = {
method: function() {return "hello world";}
};
// ES6:
var obj = {
method() {return "hello world";}
};
The above works out great because we are already inside a specialized expression when we are in an initializer list, so there is no ambiguity about whether method()
calls a function or begins the declaration of one. At this point in the expression, there is no way to call a function without a key: value
pair, so it must be the beginning of a declaration.
With your proposed idea, the only way to avoid ambiguity would be to assign a meaning to the presently errorful behavior of putting brackets after parentheses on the same line. A syntax error is currently being thrown in this case because expressions need to be deliminated either by line-breaks or by semicolons, so the brackets after the parentheses are considered part of the expression. Thus, the brackets can only be interpreted as an object initializer, which yields a syntax error due to the parentheses. Another flaw is that statements in JavaScript are generally line-ending-agnostic such that break-points in the statement can contain line-endings without affecting the functionality of the statement. Your proposed idea would only work when the whole thing is on a single line, as putting a new-line between the parentheses and the bracket would conflict with pre-established JavaScript syntax. Therefore, your proposed syntax would conflict with the present harmony of JavaScript syntax and require a redesign/overhaul of some JavaScript parsers. Again, I like your idea. However, in its present state, it does not seem very reasonable/rational.