A new function name property proposal

Brendan Eich brendan at mozilla.org
Fri Nov 16 07:31:24 PST 2012


Brandon Benvie wrote:
> This is based on 
> http://wiki.ecmascript.org/doku.php?id=strawman:name_property_of_functions
>
> Justification: the usefulness of the name of function is not just for 
> debugging. It is useful in the same ways that property names as 
> strings are such as dispatching by name or assigning by name.
>
> The goals expand a bit beyond the ones in the strawman, and are also 
> updated based on the current state of affairs. The first goal is that 
> every function has an own "name" property, and this property is always 
> a string (unless the user specifically decides to violate this norm), 
> and that this name property is initialized with the value that makes 
> sense from static semantics.The third goal is to allow predefined 
> names to be altered in cases where it makes sense.

The third goal needs more discussion. The displayName property consulted 
by certain devtools (WebKit JS profiler, IIRC; others by now) was added 
to avoid non-writable .name. With downrev browsers in the field, how can 
we make name writable and let it be used cross-browser that way?

> Semantics:
>
> The baseline for every function is the 'name' property defined as
> { value: "",
>   writable: true,
>   enumerable: false,
>   configurable: false }.

SpiderMonkey's .name property of functions is like this except for 
writable: false.

> For FunctionDeclarations, named FunctionExpressions, 
> MethodDefinitions, or accessor Properties then the function's "name" 
> property is set to the given identifier.

Nit: don't define a baseline and then mutate it. Better to have the 
write immutable pd from the get-go, for each case.

> In the case of the constructor method of classes, the class name is 
> used instead. In the case of accessors, 'get ' or 'set ' is included.

at the front, to be super-clear ;-).

> The "name" property is set to non-writable. Function.prototype's name 
> is also non-writable.

Oh, I see now: you made .name writable only as a spec-internal thing. 
But wait, what about your goal 3?

> Anonymous FunctionExpressions and ArrowFunctionExpressions assigned in 
> a VariableDeclaration or ObjectExpression

"defined in an ObjectLiteral", rather.

> are given the name of the variable or property they are assigned to 
> and the name remains writable. Anonymous ClassExpressions follow the 
> same semantics, with the name being used for the constructor method.

Really great stuff here, this makes me want to back your proposal. Just 
need to hash out the nits and points of confusion (possibly mine).

> Whenever a function's name is defined by a Symbol instead of a regular 
> identifier then the name is the result of ToString(symbol).

Symbols don't have useful ToString conversions, do they? Are you 
thinking of the optional string passed to the constructor, for 
diagnostics and debugging?

> The name property should (probably) not have any reflection on the 
> output of Function.prototype.toString.

You mean if f.name is writable then f.toString() should not reflect 
updates to f.name? Agreed, but why is f.name writable?

/be


More information about the es-discuss mailing list