Closed
Description
7.1 contains this statement:
Function declarations are named, so they're easier to identify in call stacks.
Anonymous function expressions assigned to a binding are also named, per ES6 semantics (1.e.iii). That is, the "bad" example const foo = function () { };
is the same as const foo = function foo() { };
, which is equivalent to the "good" example function foo() { }
in that respect.
Should the statement be qualified that it's only a distinction for ES5 and below ?
Activity
ljharb commentedon Mar 21, 2016
Function name inference is implemented in hardly any of the browsers we support. In addition, relying on function name inference hinders greppability, so even in a pure ES6 environment, we wouldn't recommend that.
(other subtler differences are that a function with an inferred name still doesn't have a lexical name binding in the function body, but that's not relevant here)
Arnavion commentedon Mar 21, 2016
Do you want to clarify that in the document itself? Via footnote, etc.
ljharb commentedon Mar 21, 2016
Sure, that's a great idea for a footnote!
[7.1] add link to fn names discussion. fixes #794
[7.1] add link to fn names discussion. fixes airbnb#794
[7.1] add link to fn names discussion. fixes airbnb#794
tandav commentedon Feb 26, 2017
... frome Style Guide:
So, you say, there should be 2 different names for one function?
For example,
I'm a begginer in JS/ES, can you tell, please, what are advantages of second method?
ljharb commentedon Feb 26, 2017
@tandav In your last example, the first function doesn't have a
.name
- which means in debugging, it's likely to show up as an anonymous function. In the latter example, it will show up with the namesum
- and when you grep for it, all the uses in the file will usemy_sum
, so the actual function will be easier to locate.62 remaining items