0

Possible Duplicate:
What is the most efficient way to clone a JavaScript object?
Copying an Object in Javascript

If I have this

var a = {};
a.test1 = 1;
a.test2 = 2;

and then I do

var b = a;

b.test1 = 5;

I don't want that a.test1 also changes. How can I get a new reference or object ?

Community
  • 1
  • 1
Andre Meinhold
  • 5,087
  • 3
  • 21
  • 29
  • http://www.thespanner.co.uk/2008/04/10/javascript-cloning-objects/ – androidavid Mar 07 '12 at 23:48
  • 1
    http://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-clone-a-javascript-object – Ryan Mar 07 '12 at 23:48
  • http://stackoverflow.com/questions/728360/copying-an-object-in-javascript – Ryan Mar 07 '12 at 23:48
  • Please not that `b` is a new reference to the object that `a` references. Your question title is the opposite of what you are looking for – Shad Mar 07 '12 at 23:55

1 Answers1

0

You should create object yourself and copy all properties of original object to it.

Marat Tanalin
  • 13,927
  • 1
  • 36
  • 52