在我看来,曾经有比这更短的解决方案:

let obj = { id: 12345, name: "Some name", desc: "Description" }

someFunction(id, name, desc) {
  obj.id = id;
  obj.name= name;
  obj.desc= desc;
}


有人可以告诉我是否有更快的方法来分配具有相同名称的新值?

最佳答案

我认为您正在寻找Object.assign()



let obj = {id: 12345, name: 'Some name', desc: 'Description'}
Object.assign(obj, {id: 54321})
console.log(obj) // {id: 54321, name: 'Some name', desc: 'Description'}
Object.assign(obj, {id: 123, name: 'Some other name', desc: 'New description'})
console.log(obj) // {id: 123, name: 'Some other name', desc: 'New description'}

关于javascript - ES6更改对象值的简写,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39538783/

10-13 02:36