jQuery $.extend without overriding target properties

javascript, jquery

Solution

Try something like -

for (prop in bird) {
    if(!cat.hasOwnProperty(prop)) {
        cat[prop] = bird[prop];
    }
}

After cat = {legs: 4, wings: 2}

Problem

Is it possible to extend an object without overriding the properties that are already set? In the following example I'm looking for a way to add 2 wings to the cat, but keepings it's 4 legs. ``` var cat = { legs: 4 }; var bird = { legs: 2, wings: 2 } // some references in my application to the original cat var some_cat = cat; $.extend( cat, bird ); // expected { legs: 4, wings: 2 } console.log( some_cat ); ``` Update I forgot to make it clear in the original question / example, but it is important that it modifies the original cat object.

Original source

Related problems