In Javascript some guys have this behavior and variables that receive the guy are merely references to guy initial and not independent copies.
This is the case for objects, arrays and functions. But it is not the case for numbers, strings and booleans. The problem you describe in the question does not happen with these, also called primitive types.
To solve the problem, you have different options depending on the guy what you’re dealing with. For arrays you already have an alternative in another answer, you can also use other variants that also work for objects. For complex objects you can do with ideas of these answers, the simplest way (for objects or arrays only with Primitives) is
var b = JSON.parse(JSON.stringify(a));
To copy functions the simplest way is to use new
, so you get a different instance, which shares the prototype.
It works. Thank you !
– Carlos
That’s what we’re here for!
– Brumazzi DB