As in ES2015 (ES6), functions have their own names (including the official name property), and names are assigned when the function is created in different ways in addition to the obvious function declaration and the named function expression, such as assigning variables (the function name is given as a variable name ), the assignment of object properties (the function name is set as the property name), even the default values for the function parameters (the function name is set to the parameter name). But assigning a property to an existing object (for example, not in the initializer of the object) does not assign a function property to this name. Why not? Of course, there must be a definite reason why this is undesirable / possible. What was it?
To be clear: I am not asking how to get around this. I ask what prevents this seemingly obvious case from being handled when there are so many others (including default parameter values!). There must be a good reason.
Please do not speculate or theorize. TC39 had a reason not to turn it on. I am wondering what the reason is. I went through the notes of the TC39 meeting , but have not yet found it. The closest I have found so far is Allen Wirfs-Brock answering Bergi's question to say that there was no consensus on how to do this for this form due to “various objections”, but unfortunately He did not say what these objections are.
More details:
All of the following assign functions to foo functions in a compatible browser :
But assigning a property to an existing object outside the object initializer does not:
const obj = {}; obj.foo = function() { }; console.log("Nope:", obj.foo.name);
As far as I can tell, this is described by this section of the specification, which explicitly sets the function name if the IsIdentifierRef of LeftHandSideExpression is true (which, apparently, is not for property references).
So, repeating from above: Why not? Of course, there must be a definite reason why this is undesirable / possible. What was it?
javascript function ecmascript-6
TJ Crowder Dec 12 '16 at 18:51 2016-12-12 18:51
source share