在嵌套对象上使用扩展运算符时出现错误

问题描述 投票:0回答:2

使用这样的扩展运算符时,我收到错误

Cannot redeclare block-scoped variable 'price'

const drink = {
  id: 'xhs8Pla',
  name: 'Lemonade',
};

const price = {
  sale: 99,
  full: 129,
};

const mergedDrink = { ...drink, { ...price } };

为什么会出现这个错误?

javascript ecmascript-6
2个回答
-1
投票
const drink = {
  id: 'xhs8Pla',
  name: 'Lemonade',
};

const price = {
  sale: 99,
  full: 129,
};

const mergedDrink = { ...drink, priceKey: { ...price } };

const drink = {
  id: 'xhs8Pla',
  name: 'Lemonade',
};

const price = {
  sale: 99,
  full: 129,
};

const mergedDrink = { ...drink, ...price};

-1
投票

由于语法错误而出现错误。

在嵌套对象中使用展开运算符时,展开应如下所示:

const mergedDrink = { ...drink, ...{ price } };

© www.soinside.com 2019 - 2024. All rights reserved.