Why exported arrow functions DO NOT save their name and is there any way to do this?
Give the ES6 module as shown below:
myModule.js
export function doSomeWork1() {
}
export const doSomeWork2 = () => {
};
If I then import them into another file, so that the exported function retains its name, and the arrow function does not work.
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
conclusion:
doSomeWork1...
[Function: doSomeWork1]
doSomeWork1
doSomeWork2...
[Function]
If I were to declare the arrow function in the ES6 module without exporting it and print it directly in this file, it will save its name.
myModule.js
const doSomeWork3 = () => {
};
console.log('doSomeWork3...');
console.log(doSomeWork3);
console.log(doSomeWork3.name); // name is retained
conclusion:
doSomeWork3...
[Function: doSomeWork3]
doSomeWork3
source
share