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
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With