By default, $.extend just a shallow copy * property. This is not very interesting.
In this case, the extended properties are: "0", "1", "2", etc., and therefore shat[0] === shot[0] is true after the extension , which takes place only if the same object. Since they are one and the same object, then ... well, mutating it, one place mutates it everywhere.
To separate them, consider a “deep copy” (see the various ways to call jQuery.extend ). Another approach that I mention, since it can also be used in other contexts, is to serialize the object and then de-serialize it. with JSON.
var shat = [{bang:true}, {bumm: false}, {bong: false}] var shot = [{bang:false}, {bumm: false}, {bong: false}] $.extend(true, shat, shot) // use "deep copy" shat[0] === shot[0] // false; different objects now so... shot[0].bang = "boom" // ...this will not affect the object named by shat[0] shot[0].bang // "boom" shat[0].bang // false
Happy coding.
* That is, for each p property in the source object (s), it simply executes target[p] = source[p] . Remember that in JavaScript, arrays are just a special subtype of an object with a magic length : the value at index n is called the "n" property.
user166390
source share