Skip to content

7.1 says function expressions are always unnamed, but this changed in ES6 #794

Closed
@Arnavion

Description

@Arnavion

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

ljharb commented on Mar 21, 2016

@ljharb
Collaborator

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

Arnavion commented on Mar 21, 2016

@Arnavion
ContributorAuthor

Do you want to clarify that in the document itself? Via footnote, etc.

ljharb

ljharb commented on Mar 21, 2016

@ljharb
Collaborator

Sure, that's a great idea for a footnote!

reopened this on Mar 21, 2016
added a commit that references this issue on Aug 26, 2016
1541503
added a commit that references this issue on Dec 6, 2016
f9d8629
added a commit that references this issue on Jan 22, 2017
c72b418
tandav

tandav commented on Feb 26, 2017

@tandav

... frome Style Guide:

// bad
const foo = function () {
  // ...
};

// good
const foo = function bar() {
  // ...
};

So, you say, there should be 2 different names for one function?

For example,

// Is it worse
const sum = function(a, b) {
  return a + b;
};

// than this?
const my_sum = function sum(a, b) {
  return a + b;
};

I'm a begginer in JS/ES, can you tell, please, what are advantages of second method?

ljharb

ljharb commented on Feb 26, 2017

@ljharb
Collaborator

@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 name sum - and when you grep for it, all the uses in the file will use my_sum, so the actual function will be easier to locate.

62 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @ljharb@chengyin@teohhanhui@CinchBlue@Arnavion

      Issue actions

        7.1 says function expressions are always unnamed, but this changed in ES6 · Issue #794 · airbnb/javascript