๐Ÿ’พobj.mergeWith()

JS โŸฉ object โŸฉ built-in โŸฉ Object โŸฉ extension โŸฉ obj.mergeWith(...sources)

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);
        },
    },

});

Last updated