为什么导出的箭头功能不会保留其名称,是否有办法让他们这样做?
给出如下的ES6模块:
myModule.js
export function doSomeWork1() {
}
export const doSomeWork2 = () => {
};
如果我然后将它们导入到另一个文件中,那么导出的函数会保留其名称,而箭头函数则不会。
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
输出:
doSomeWork1...
[Function: doSomeWork1]
doSomeWork1
doSomeWork2...
[Function]
如果我要在ES6模块中声明箭头功能而不导出它并直接在该文件中打印出来,它会保留它的名称。
myModule.js
const doSomeWork3 = () => {
};
console.log('doSomeWork3...');
console.log(doSomeWork3);
console.log(doSomeWork3.name); // name is retained
输出:
doSomeWork3...
[Function: doSomeWork3]
doSomeWork3
答案 0 :(得分:1)
我刚尝试了,并且还导出了让func123 =()=> {} 它适用于" const"并为"让"太。 也许是你正在使用的一些配置?你在使用webpack还是什么?