> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/js/val/builtin/object/ext.md).

# Object extension

[JS](/web/js.md) ⟩ [object](/web/js/val/obj.md) ⟩ [built-in](/web/js/val/builtin.md) ⟩ [Object](/web/js/val/builtin/object.md) ⟩ extension&#x20;

{% tabs %}
{% tab title="🔴 主題" %}

* [obj.mergeWith()](/web/js/val/builtin/object/ext/merge-with.md) - copy (own enumerable) properties from other sources.
  {% endtab %}

{% tab title="💾 程式" %}

* replit ⟩ [Object\_ext.js](https://replit.com/@pegasusroe/play-nodejs#ext/Object_ext.js)

```javascript
// ⭐️ Global Functions

// check if value is object
function isObject(value) { return value === Object(value) }

// check if value is primitive
function isPrimitive(value) { return !isObject(value) }


// ⭐️ extending Object.prototype
// -------------------------------------
//    method/accessor      parameters
// -------------------------------------
// 🔹 .defineProperty     (prop, desc)
// 🔹 .defineProperties   (desc)
// 🔹 .propertyDescriptor (prop)
// 🔹 .assign             (...sources)
// 🔹 .mergeWith          (...sources)
// 🔸 .prototype
// 🔸 .keys
// 🔸 .symbols

Object.defineProperties(Object.prototype, {

    // 🔹 obj.defineProperty(prop, desc)
    defineProperty: {
        value: function(prop, descr) {
            return Object.defineProperty(this, prop, descr);
        },
    },

    // 🔹 obj.defineProperties(desc)
    defineProperties: {
        value: function(descr) {
            return Object.defineProperties(this, descr);
        },
    },

    // 🔹 obj.propertyDescriptor(prop)
    propertyDescriptor: {
        value: function(prop) {
            return Object.getOwnPropertyDescriptor(this, prop);
        },
    },

    // 🔹 obj.assign(...sources)
    assign: {
        value: function(...sources) {
            return Object.assign(this, ...sources);
        },
    },

    // 🔹 obj.mergeWith(...sources)
    mergeWith: {
        value: function(...sources) {

            // prepare to collect descriptors
            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 and return
            return this.defineProperties(descriptors);
        },
    },

    // 🔸 obj.prototype
    prototype: {
        get: function() {
            return Object.getPrototypeOf(this);
        },
    },

    // 🔸 obj.keys (own enumerable)
    keys: {
        get: function() {
            return Object.keys(this);
        },
    },

    // 🔸 obj.symbols (own enumerable/non-enumerable)
    symbols: {
        get: function() {
            return Object.getOwnPropertySymbols(this);
        },
    },

});

// export
module.exports = { 
    isObject, 
    isPrimitive,
};
```

{% endtab %}

{% tab title="⬇️ 應用" %}

* property [attribute](/web/js/val/obj/prop/attr.md)(s) of an object
  {% endtab %}

{% tab title="📘 手冊" %}

* [Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)
  {% endtab %}
  {% endtabs %}
