ES6从对象导出所有值 [英] ES6 export all values from object
问题描述
./ my-module.js
),它有一个对象应该是它的返回值: let values = {a:1,b:2,c:3}
//export values导致SyntaxError:Unexpected令牌
所以我可以导入它们,如:
import'a'from'./my-module'// a === 1
import * as myModule from'./my-module'// myModule。 a === 1
我发现的唯一方法是通过硬编码导出:
export let a = values.a
export let b = values.b
export let c = values.c
//或:
export let {a,b,c} = values
是否可以从对象导出所有值?
似乎没有。报价从 ECMAScript 6模块:最终语法:
您可能会想知道 - 如果我们只能使用默认输出对象(如CommonJS),为什么需要命名输出?答案是您不能通过对象强制执行静态结构,并失去所有相关优势(在下一节中描述)。
Say I have a module (./my-module.js
) that has an object which should be its return value:
let values = { a: 1, b: 2, c: 3 }
// "export values" results in SyntaxError: Unexpected token
So I can import them like:
import {a} from './my-module' // a === 1
import * as myModule from './my-module' // myModule.a === 1
The only way I found is by hard coding the exports:
export let a = values.a
export let b = values.b
export let c = values.c
// or:
export let {a, b, c} = values
Which is not dynamic.
Is it possible to export all values from an object?
Does not seem so. Quote from ECMAScript 6 modules: the final syntax:
You may be wondering – why do we need named exports if we could simply default-export objects (like CommonJS)? The answer is that you can’t enforce a static structure via objects and lose all of the associated advantages (described in the next section).
这篇关于ES6从对象导出所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!