简体   繁体   中英

Is destructured es6 export a valid syntax?

I am wondering about the following syntax:

export const { foo, bar } = {
  foo: 123,
  bar: 234,
}

I can't find it described in any es2015 documentation , but it is supported by Babel's es2015 preset .

Is this syntax truly standard, as babel seems to think? Out of standard?

There are several things going on here.

export const { foo, bar } = {
  foo: 123,
  bar: 234,
};

is equivalent to

const { foo, bar } = {
  foo: 123,
  bar: 234,
};

// Export all the names in the variable declaration.
export { foo as foo, bar as bar };

export can be used on many declarations and will export whatever variables are created by that declaration.

Then this is simplified further if you don't understand destructuring, it is essentially

const _tmp = {
  foo: 123,
  bar: 234,
};
const foo = _tmp.foo,
      bar = _tmp.bar;

// Export all the names in the variable declaration.
export { foo as foo, bar as bar };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM