为什么我不能在ES6中导出导出声明的函数? [英] why I can't export export a declared function in ES6?
本文介绍了为什么我不能在ES6中导出导出声明的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经阅读了MDN的文档,好吧,主要是关于新模块功能的好处,让我感到困惑的是有关 export
的小东西,现在,让我们来看一下:
I have read docs from MDN, ok, mainly it's good about the new module feature, what makes me confused is the small things about export
, now, let's see:
当我
export function foo(x) {
return x * x;
}
或
export const foo = (x) => {
return x * x
}
都可行;
但如果
const foo = (x) => {
return x * x
}
export foo // failed
我知道这里应该是 export {foo}
,但是,为什么呢?有什么区别,应该可以.很高兴听到一些精灵的想法.
I know here should be export {foo}
, but, why? what's the difference, that should be work. glad to hear some genies ideas.
推荐答案
ES模块仅支持几种语法变体以便进行静态分析.
ES modules support only several syntax variations in order to be statically analyzed.
根据参考,变体为:
export { name1, name2, …, nameN };
export { variable1 as name1, variable2 as name2, …, nameN };
export let name1, name2, …, nameN; // also var, function
export let name1 = …, name2 = …, …, nameN; // also var, const
export default expression;
export default function (…) { … } // also class, function*
export default function name1(…) { … } // also class, function*
export { name1 as default, … };
export * from …;
export { name1, name2, …, nameN } from …;
export { import1 as name1, import2 as name2, …, nameN } from …;
export foo
不在其中.不支持,无法使用.
export foo
is not among them. It is not supported and cannot be used.
这篇关于为什么我不能在ES6中导出导出声明的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文