具有功能参数的对象破坏

时间:2019-04-17 21:18:27

标签: javascript arrays function object ecmascript-6

如果我具有以下ES6方法:

function myClothes([first, second, third]) {
  return {
    first: first,
    second: second,
    third: third
  }
} 

如何在控制台窗口中打印出“运动鞋裤子衬衫”?我已经尝试了以下方法,但是仍然缺少解决方案:

console.log(myClothes({
  ['first']:'sneakers',
  ['second']:'pants',
  ['third']:'shirt'
}));

我在做什么错了?

非常感谢!

1 个答案:

答案 0 :(得分:1)

首先,将函数更改为使用对象分解。然后在函数内使用简单的console.log语句:

function myClothes({first, second, third}) {
  console.log([first, second, third].join(" "));
  return {
    first: first,
    second: second,
    third: third
  }
}

console.log(myClothes({
  ['first']:'sneakers',
  ['second']:'pants',
  ['third']:'shirt'
}));
.as-console-wrapper { max-height: 100% !important; top: auto; }