💾 add default properties
錯誤做法:以下的做法,obj 原來的屬性反而會被預設值覆蓋掉❗️
Object.assign(obj, defaults) // ❌
正確做法:
// ╭╮ <-- start with a new object
obj = Object.assign({}, defaults, obj) // ✅
// ╰─╯ <-- override defaults with obj
也可以用 Spread syntax (...) 來寫:
obj = {...defaults, ...obj}
Last updated
Was this helpful?