3

Quick question: what's the correct result for this code:

let f = function(){};
let n = f.name; //"" or "f"?

According to the compat table, n should have the value "f". However, the mozilla docs say that it should return an empty string. Which one is correct?

Paul S.
  • 64,864
  • 9
  • 122
  • 138
Luis Abreu
  • 4,008
  • 9
  • 34
  • 63
  • 3
    the function is anonymous, so I can't imagine why it should have name "f"... – MightyPork Dec 14 '14 at 14:18
  • I'm under the impression that the name property is there to help us debug code. So, it would make sense for it to be called f after being assigned to a variable, but I'm not sure. There's also the question of having something like this: let n2 = n; //so, what should name return now? – Luis Abreu Dec 14 '14 at 14:22

2 Answers2

3

Since ECMAScript 6 is currently in draft state, the answer below may become outdated sometime in the future.
That being said, referencing the spec draft:

Anonymous functions objects that do not have a contextual name associated with them by this specification do not have a name own property but inherit the name property of %FunctionPrototype%.

The ECMAScript 6 Wiki reads that

If no name can be statically determined, such as in the case of an unassigned anonymous function, then the empty string is used.

however,

Some functions are anonymous and have no name given as part of their static semantics. If the function is directly assigned to a LHS where a name is statically determinable then the LHS name is used.

Note that the claims made by the wiki aren't referenced (and can't directly be found) in the spec draft, but they're reasonable assumptions.

If we take those assumptions to be true, the result of your sample function call would be "f", since the anonymous function is assigned to a LHS.
Reading the name property of an unassigned anonymous function should return an empty string.

Etheryte
  • 24,589
  • 11
  • 71
  • 116
  • I don't know why they're thinking about this on the wiki, you can give names to function expressions if you want them to have names, perhaps it would be better to make `.name` writable. What happens when you do `var fn = (function () {return function () {}}());` Is that still _directly assigned to a LHS_? Will I ever be able to reference anonymous functions ever again without letting them get "names"? :'( – Paul S. Dec 14 '14 at 14:37
  • I'd say that fn.name would still return "fn", no? – Luis Abreu Dec 14 '14 at 14:40
  • 1
    @PaulS., in that case the RHS is not a function literal syntactically, so no name is set. The draft spec is pretty explicit about this. – Andreas Rossberg Dec 14 '14 at 17:00
1

It will return "f", in your example, as well as in other variations:

let f = function(){}
const f = function(){}
var f = function(){}
f = function(){}  // assignment
let f = () => {}
// etc.

The relevant bits in ES6 spec draft are all the occurrences of SetFunctionName. In the case of your example, see its invocation in Section 13.2.1.4. It applies only when the RHS syntactically is an anonymous function literal.

Andreas Rossberg
  • 34,518
  • 3
  • 61
  • 72