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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744380874a4571422.html
评论列表(0条)