1

I am facing a strange problem in JavasSript.

There are three variables (declared as var), they all store an object; say var object1, object2 and object3.

Ok, so what I do now is I assign object2=object1 and object3=object1.

And when I add something to object2, it automatically gets added to object3. Strange? :|

Please help. I think I am missing some basic funda here, but I am not able to catch it.

katspaugh
  • 17,449
  • 11
  • 66
  • 103
Anupam
  • 1,821
  • 3
  • 23
  • 41
  • 1
    Well yes, an `Object` in Javascript is passed by reference. `object2=object1` changes the `object2` variable to reference the object in `object1`, it doesn't copy it. – James M Feb 18 '12 at 12:34
  • 1
    possible duplicate of [Does javascript handle objects by reference or by value?](http://stackoverflow.com/questions/2267239/does-javascript-handle-objects-by-reference-or-by-value) – epascarello Feb 18 '12 at 12:35
  • How can I make sure object3 doesn't get modified when object2 is modified? – Anupam Feb 18 '12 at 12:35
  • @JamesMcLaughlin: They are **not** passed by reference (pass by reference has very specific and different meaning). But yes, they are reference types and all three variables store things called references. The terminology here is awful. –  Feb 18 '12 at 12:45

3 Answers3

2

When you assign opject2 = object1, you are not making a copy of object1, you are simply asking that both names refer to the same object. You need to make a new object and copy the attributes from object1 into it. How you do that depends on what Javascript libraries you have available to you.

jQuery provides copying techniques described here

Community
  • 1
  • 1
Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • Well, I have done something like the following : object3=new Object() and then object3=object1. But the problem stays? Any means of copying it if I dont know the attributes of the object? I have access to jquery 1.7.1; can this do any help? – Anupam Feb 18 '12 at 12:37
  • Not true for all types of variables, see @katspaugh's answer. – Julian D. Feb 18 '12 at 12:49
2

So how is that strange?

You pass reference to the object and do not clone the object. So any modification of your variables object1 to object3 actually refer to the same object, thus "automatically updating all variables".

Sirko
  • 72,589
  • 19
  • 149
  • 183
  • How can I clone the object then without knowing the attributes of the Object? Thanks.. – Anupam Feb 18 '12 at 12:38
  • have a look at the [answers here](http://stackoverflow.com/questions/728360/copying-an-object-in-javascript). – Sirko Feb 18 '12 at 12:41
  • Thanks man, I did this and it works like a charm! var newObject = jQuery.extend(true, {}, oldObject); – Anupam Feb 18 '12 at 12:45
1

you have to clone the object or use Object.create(object) function and pass the object so it will create new one object not assign the reference

see images

var obj1 = {key: 1}
var obj2 = Object.assign({},obj1)

enter image description here

kuldeep chopra
  • 652
  • 9
  • 9