When i use a Fat Arrow as function for object keys webpack compiles it like this...
// Before compiling
{
key1: (arg1) => {
// ...
}
}
// After compiling ['es2015']
{
key1: function key1(arg1) {
// ...
}
}
Why is "key1" created twice ?
It should compile like
{
key1: function(arg1) {
// ...
}
}
This problem is babel core.
Please ask a question in babel.
FYI: https://github.com/babel/babel#looking-for-support
Thanks.
I think this is done so that stacktraces would show 'key1' and not 'anonymous function'. What is the problem you have with it?
Functions that are anonymous automatically get a name based on where they are declared, so in a real ES6 environment you see
var obj = {
key1: (arg1) => {}
};
console.log(obj.key1.name); // 'key1'
that is the behavior being emulated here.
From http://www.ecma-international.org/ecma-262/7.0/#sec-object-initializer-runtime-semantics-propertydefinitionevaluation
PropertyDefinition: PropertyName: AssignmentExpression
- Let propKey be the result of evaluating PropertyName.
- ReturnIfAbrupt(propKey).
- Let exprValueRef be the result of evaluating AssignmentExpression.
- Let propValue be ? GetValue(exprValueRef).
- If IsAnonymousFunctionDefinition(AssignmentExpression) is true, then
- Let hasNameProperty be ? HasOwnProperty(propValue, "name").
- If hasNameProperty is false, perform SetFunctionName(propValue, propKey).
- Assert: enumerable is true.
- Return CreateDataPropertyOrThrow(object, propKey, propValue).
Specifically step 5 assigns names to any anonymous object properties based on the name of the property.
Most helpful comment
Functions that are anonymous automatically get a name based on where they are declared, so in a real ES6 environment you see
that is the behavior being emulated here.
From http://www.ecma-international.org/ecma-262/7.0/#sec-object-initializer-runtime-semantics-propertydefinitionevaluation
Specifically step 5 assigns names to any anonymous object properties based on the name of the property.