Javascript 是否可以在 ES6/7 中导出箭头函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36458697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:06:00  来源:igfitidea点击:

Is it possible to export Arrow functions in ES6/7?

javascriptecmascript-6arrow-functions

提问by jozzy

The export statement below gives a syntax error

下面的导出语句给出了一个语法错误

export default const hello = () => console.log("say hello")

why ?

为什么 ?

I'm only able to export named functions

我只能导出命名函数

export function hello() {
  console.log("hello")
}

What is the reason?

是什么原因?

回答by Felix Kling

Is it possible to export Arrow functions in ES6/7?

是否可以在 ES6/7 中导出箭头函数?

Yes. exportdoesn't care about the value you want to export.

是的。export不关心您要导出的值。

The export statement below gives a syntax error ... why?

下面的导出语句给出了一个语法错误......为什么?

You cannot have a defaultexport andgive it a name("default" is already the name of the export).

您不能拥有默认导出为其命名(“默认”已经是导出的名称)。

Either do

要么做

export default () => console.log("say hello");

or

或者

const hello = () => console.log("say hello");
export default hello;

回答by Raphael Pinel

If you don't want a default export, you can simply export a named function with this syntax:

如果您不想要默认导出,则可以使用以下语法简单地导出命名函数:

export const yourFunctionName = () => console.log("say hello");