Keywords as method names

# Jason Orendorff (12 years ago)

The ES6 draft says:

MethodDefinition : PropertyName ( StrictFormalParameters ) { FunctionBody }
PropertyName : IdentifierName

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?

# Tab Atkins Jr. (12 years ago)

Presumably because you can't call them without being ambiguous with the keyword.

# Andrea Giammarchi (12 years ago)

window.if = boom;

if you really want to ... otherwise feel free to mess up with a with({if:yourFunc}){} block ^_^

# David Herman (12 years ago)

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.

# Brendan Eich (12 years ago)

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.

# David Bruant (12 years ago)

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.