Keywords as method names
Presumably because you can't call them without being ambiguous with the keyword.
window.if = boom;
if you really want to ... otherwise feel free to mess up with a
with({if:yourFunc}){}
block ^_^
In Lisp it makes sense to allow binding keywords because there's no such thing as a keyword: once you bind it, it's a variable and you can refer to it. In JS it's impossible to refer to it as a variable so it's just an (un)attractive nuisance.
The only place where I could see this being arguably useful in ES6 is:
// foo.js
export function function() { }
// main.js
module foo from "foo";
foo.function();
But that's still probably not advisable, since it means you can't import the name:
import { function } from "foo";
// d'oh
So I don't see any reason why a person shouldn't just use rebinding for that use case:
// foo.js
function function_() { }
export { function_ as function };
To wit: I say leave it out.
David Herman wrote:
To wit: I say leave it out.
This was TC39's consensus when I raised the option based on SpiderMonkey (from ES4 days) allowing keywords as function names. (We still carry that extension, BTW.) So, no change to ES6 status quo.
Le 22/08/2013 02:23, Brendan Eich a écrit :
David Herman wrote:
To wit: I say leave it out.
This was TC39's consensus when I raised the option based on SpiderMonkey (from ES4 days) allowing keywords as function names. (We still carry that extension, BTW.)
Filed bugzilla.mozilla.org/show_bug.cgi?id=908133, ecmascript#1798 to keep all that in mind.
The ES6 draft says:
This means a method name can be a keyword:
obj = {if() {}}
. This is consistent with other property names ({if: true}
is allowed), but inconsistent with other function names (function if(){}
is not allowed).Why not allow keywords as function names, too?