admin管理员组文章数量:1403221
In ES6, we can import exported modules like this:
import { Abc } from './file-1'; // here Abc is a named export
import Def from './file-2'; // here Def is the default export
But how can we import anonymous functions? Consider this code:
file-3.js:
export function() {
return 'Hello';
}
export function() {
return 'How are you doing?';
}
How can I import above two functions in another file, given that neither are they default exports nor are they named exports (anonymous functions don't have names!)?
In ES6, we can import exported modules like this:
import { Abc } from './file-1'; // here Abc is a named export
import Def from './file-2'; // here Def is the default export
But how can we import anonymous functions? Consider this code:
file-3.js:
export function() {
return 'Hello';
}
export function() {
return 'How are you doing?';
}
How can I import above two functions in another file, given that neither are they default exports nor are they named exports (anonymous functions don't have names!)?
Share Improve this question asked Apr 22, 2018 at 17:53 darKnightdarKnight 6,48115 gold badges58 silver badges93 bronze badges 6- 3 You can't do this. – Evert Commented Apr 22, 2018 at 17:56
- You put them in different files. – iSkore Commented Apr 22, 2018 at 17:57
- file-3.js is syntactically invalid. – Ry- ♦ Commented Apr 22, 2018 at 17:57
- Why would you even want to do it? What's the real problem you're trying to solve? – JJJ Commented Apr 22, 2018 at 17:57
-
@Ry︁: Why is
file-3.js
syntactically invalid? – darKnight Commented Apr 22, 2018 at 18:07
1 Answer
Reset to default 6Single anonymous function can be exported as default
(default export value can be anything). Multiple anonymous functions cannot be exported from a module - so they cannot be imported, too.
export
statement follows strict syntax that supports either named or default exports. This will result in syntax error:
export function() {
return 'Hello';
}
These functions should be named exports:
export const foo = function () {
return 'Hello';
}
export const bar = function () {
return 'How are you doing?';
}
So they can be imported under same names.
本文标签: javascriptHow to import anonymous functions in ES6Stack Overflow
版权声明:本文标题:javascript - How to import anonymous functions in ES6 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744380874a2603497.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论