1

I am using Google Chrome version 52 64-bit. I found out that if I use anonymous function expression ex.

//  Anonymous function expression 
var expressionFunc = function(){
    return true;
};

The variable expressionFunc will hold the assigned anonymous function, But it is also adding a name property expressionFunc to this function. So if I do expressionFunc.name in the console, It will give me expressionFunc.

From what I know this anonymous function expression should stay anonymous, And the function referenced by the variable should not contain the variable name in the name property of the function.

Why is chrome assigning name property to an anonymous function?

RuvenGam
  • 64
  • 1
  • 9
  • It's my understanding that you're not creating an anonymous function here, you're just declaring the function with different syntax. Happy for someone who knows the standard a bit more to elaborate. An anonymous function would be more like (function () { alert(true); })(); – Liam MacDonald Sep 01 '16 at 10:36
  • 1
    The behaviour in Firefox is different, suggesting that it's not clear-cut. – Chris Lear Sep 01 '16 at 10:39
  • This is called inline anonymous function expression. this function do not have a name so it is anonymous inline function and the assignment to the variable makes it an inline anonymous function expression – RuvenGam Sep 01 '16 at 10:41
  • Sorry forgot to add: `(function () { alert(true); })();` Your function is called self invoking anonymous function. – RuvenGam Sep 01 '16 at 10:50
  • This code: `var expressionFunc = function somename(){}; console.log expressionFunc.name;` shows `somename` in all browsers. Only Chrome gives a name to the anonymous function in the original question. (Browsers tried: Edge, Firefox, Opera, Chrome) – Chris Lear Sep 01 '16 at 11:01
  • Yes, it's part of the not-so-new-any-more ES6 standard: http://stackoverflow.com/a/37488652/1048572 – Bergi Sep 01 '16 at 11:43

1 Answers1

0

This page:

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Function/name

Says this

Browsers that implement ES6 functions can infer the name of an anonymous function from its syntactic position. For example:

var f = function() {};
console.log(f.name); // "f"

There's no particular explanation on that page.

This page

http://www.2ality.com/2015/09/function-names-es6.html

Says this

With regard to names, arrow functions are like anonymous function expressions:

     const func = () => {};
     console.log(func.name); // func

From now on, whenever you see an anonymous function expression, you can assume that an arrow function works the same way.

The answer at https://stackoverflow.com/a/37488652/1048572 (referenced by @bergi) is pretty comprehensive, and points to the source in the specification.

Community
  • 1
  • 1
Chris Lear
  • 6,592
  • 1
  • 18
  • 26
  • Notice that browsers did this inference since long for stack traces, it's only since ES6 that they make it available as a property and that there are set rules for when to do it. – Bergi Sep 01 '16 at 11:44