我认为您正在寻找该
with声明,它完全符合您的要求:
const vegetableColors = {corn: 'yellow', peas: 'green'};with (vegetableColors) { console.log(corn);// yellow console.log(peas);// green}但是,出于充分的原因, 不推荐使用 (在严格模式下,其中包括ES6模块)。
将所有属性分解为当前范围
您无法在ES61中使用。那是一件好事。明确介绍要引入的变量:
const {corn, peas} = vegetableColors;或者,可以使用扩展全局对象,
Object.assign(global,vegetableColors)以将它们置于全局范围内,但实际上,这比
with声明要糟糕。
1:…虽然我不知道ES7中是否有允许这种事情的草案,但我可以告诉您,任何提案都会被TC否决:-)



