# obj.mergeWith()

[JS](https://lochiwei.gitbook.io/web/js) ⟩ [object](https://lochiwei.gitbook.io/web/js/val/obj) ⟩ [built-in](https://lochiwei.gitbook.io/web/js/val/builtin) ⟩ [Object](https://lochiwei.gitbook.io/web/js/val/builtin/object) ⟩ [extension](https://lochiwei.gitbook.io/web/js/val/builtin/object/ext) ⟩ obj.mergeWith(...sources)

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

* replit ⟩ [obj.mergeWith(...sources)](https://replit.com/@pegasusroe/objmergeWithsources#ext/Object_ext.js), compare with [.assigndescriptors](https://lochiwei.gitbook.io/web/js/val/obj/extend/mixin/.assigndescriptors "mention")
* require [](https://lochiwei.gitbook.io/web/js/val/builtin/object/ext "mention")

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

});
```

{% endtab %}

{% tab title="💈範例" %}

* [replit](https://replit.com/@pegasusroe/objmergeWithsources#index.js), require [](https://lochiwei.gitbook.io/web/js/val/builtin/object/ext "mention")

```javascript
const source = {
  x: 1,                  // data property
  get y() { return 2 },  // accessor (getter)
};

({}).assign(source)      // { x: 1, y: 2 }
({}).mergeWith(source)   // { x: 1, y: [Getter] }
```

{% endtab %}

{% tab title="👥 相關" %}

* compare with [object.assign](https://lochiwei.gitbook.io/web/js/val/obj/extend/object.assign "mention")
* [extend](https://lochiwei.gitbook.io/web/js/val/obj/extend "mention")
* this method needs to [enumerate](https://lochiwei.gitbook.io/web/js/val/obj/prop/enumerate "mention") and deal with their [attr](https://lochiwei.gitbook.io/web/js/val/obj/prop/attr "mention")(s).
  {% endtab %}

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

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lochiwei.gitbook.io/web/js/val/builtin/object/ext/merge-with.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
