最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ES6's export and curly braces - Stack Overflow

programmeradmin5浏览0评论

I saw a code got posted in a chat channel. At the very end of his code is

export {UserInformation};

There were groups saying that the syntax is wrong. Some were saying it is fine as long as the variable exists.

So which group is right? It's my first time seeing this kind of syntax too. I've never seen curly braces in export. I've only used them in import. Like this

import {method} from 'someModule';

If I was writing it, I would write it as

export default UserInformation;

I don't want to pollute my brain with wrong information. Let me know which export is correct.

I saw a code got posted in a chat channel. At the very end of his code is

export {UserInformation};

There were groups saying that the syntax is wrong. Some were saying it is fine as long as the variable exists.

So which group is right? It's my first time seeing this kind of syntax too. I've never seen curly braces in export. I've only used them in import. Like this

import {method} from 'someModule';

If I was writing it, I would write it as

export default UserInformation;

I don't want to pollute my brain with wrong information. Let me know which export is correct.

Share Improve this question asked Jan 8, 2016 at 3:04 devwannabedevwannabe 3,2108 gold badges44 silver badges82 bronze badges 1
  • Why don't you just try it? Or, as an alternative, you could read the documentation at developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…. The very first example given is export { name1, name2, …, nameN };. – user663031 Commented Jan 8, 2016 at 3:36
Add a comment  | 

1 Answer 1

Reset to default 26

The syntax is correct. This

export {UserInformation};

is shorthand for

export {UserInformation as UserInformation};

which is like doing

export const UserInformation = {};

when you define UserInformation.

It's useful to be able to export something from a module in a different place where it's defined (for readability, for instance).

In this case, you'd import UserInformation like this

import {UserInformation} from 'UserInformation.js';

Please note that export default UserInformation; is not equivalent to this. In that case, you're making UserInformation be the default module export. To import UserInformation in that case, you'd do:

import UserInformation from 'UserInformation.js';

which is shorthand for

import {default as UserInformation} from 'UserInformation.js';

This blog post is an excellent read about the topic.

发布评论

评论列表(0)

  1. 暂无评论