Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 Module arrow function exports don't retain their name

Why do exported arrow functions NOT retain their name and is there a way to have them do so?

Give an ES6 Module like below:

myModule.js

export function doSomeWork1() {
}
export const doSomeWork2 = () => {
};

If I then import them into another file like so the exported function retains its name while the arrow function does not.

example.js

import { doSomeWork1, doSomeWork2 } from './myModule';
console.log('doSomeWork1...');
console.log(doSomeWork1);
console.log(doSomeWork1.name); // name is retained    
console.log('doSomeWork2...');
console.log(doSomeWork2);
console.log(doSomeWork2.name); // name is NOT retained

output:

doSomeWork1...
[Function: doSomeWork1]
doSomeWork1
doSomeWork2...
[Function]

If I were to declare an arrow function in an ES6 Module without exporting it and print it out directly in that file it does retain it's name.

myModule.js

const doSomeWork3 = () => {
};
console.log('doSomeWork3...');
console.log(doSomeWork3);
console.log(doSomeWork3.name); // name is retained 

output:

doSomeWork3...
[Function: doSomeWork3]
doSomeWork3
like image 595
TugboatCaptain Avatar asked May 20 '17 23:05

TugboatCaptain


1 Answers

I just tried that and also export let func123 = () => {} and it worked for "const" and for "let" too. maybe is some configuration that you are using? are you using webpack or something?

like image 84
UXDart Avatar answered Nov 15 '22 09:11

UXDart