30秒学会 JavaScript 片段 · 2023年3月17日

30秒学会 JavaScript 片段 – Assign default values for object properties

Assigns default values for all properties in an object that are undefined.

  • Use Object.assign() to create a new empty object and copy the original one to maintain key order.
  • Use Array.prototype.reverse() and the spread operator (...) to combine the default values from left to right.
  • Finally, use obj again to overwrite properties that originally had a value.

代码实现

const defaults = (obj, ...defs) =>
  Object.assign({}, obj, ...defs.reverse(), obj);

defaults({ a: 1 }, { b: 2 }, { b: 6 }, { a: 3 }); // { a: 1, b: 2 }

翻译自:https://www.30secondsofcode.org/js/s/default-values-for-object-properties