javascript - How to import anonymous functions in ES6 - Stack Overflow

In ES6, we can import exported modules like this:import { Abc } from '.file-1'; here Abc

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
 |  Show 1 more ment

1 Answer 1

Reset to default 6

Single 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

相关推荐

  • javascript - How to import anonymous functions in ES6 - Stack Overflow

    In ES6, we can import exported modules like this:import { Abc } from '.file-1'; here Abc

    8天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信