> 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/obj/extend/mixin/mergewithoutoverride.md).

# mergeWithoutOverride()

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

* [replit](https://replit.com/@pegasusroe/merge-without-override#index.js)

```javascript
// like Object.assign() but different
function mergeWithoutOverride(target, ...sources) {

    for (let source of sources) {
        // doesn't handle Symbol properties
        for (let key of Object.keys(source)) { 
            // doesn't override existing properties
            if (!(key in target)) target[key] = source[key];
        }
    }
    
    return target;
}
```

💈範例：

```javascript
Object.assign({x:1}, {x:2, y:2}, {x:3, y:3, z:3})
// { x: 3, y: 3, z: 3 } (⭐️ override with new values)

mergeWithoutOverride({x:1}, {x:2, y:2}, {x:3, y:3, z:3})
// { x: 1, y: 2, z: 3 } (⭐️ keep old values)
```

{% endtab %}

{% tab title="📗 參考" %}

* [x] (2020) JavaScript: The Definitive Guide, 7th Edition (6.7 Extending Objects)
  {% endtab %}

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

* like [Object.assign()](/web/js/val/obj/extend/object.assign.md) but different.
  {% endtab %}
  {% endtabs %}
