Object.defineProperties(Object.prototype, {
// ๐น other related methods omitted ...
// ๐น obj.mergeWith(...sources)
mergeWith: {
value: function(...sources) {
const descriptors = {};
// collect all descriptors from sources
sources.forEach(source => {
// own enumerable (string-keyed) properties -> descriptors
source.keys.forEach(key => {
descriptors[key] = source.propertyDescriptor(key);
});
// own enumerable (symbol-keyed) properties -> descriptors
source.symbols.forEach(sym => {
const desc = source.propertyDescriptor(sym);
if (desc.enumerable) descriptors[sym] = desc;
});
});
// merge
return this.defineProperties(descriptors);
},
},
});
const source = {
x: 1, // data property
get y() { return 2 }, // accessor (getter)
};
({}).assign(source) // { x: 1, y: 2 }
({}).mergeWith(source) // { x: 1, y: [Getter] }