7

Are javascript objects value based or reference based? For example:

obj1.list =new array();
// ------ populate list
obj2.list = obj1.list

Does the browser make different copy of the obj1.list for obj2.list, or is obj2.list just a reference to obj1.list?

Matt W-D
  • 1,605
  • 2
  • 19
  • 22
Arslan Ahson
  • 256
  • 1
  • 3
  • 10
  • 1
    possible duplicate of [Does Javascript equal sign reference objects or clones them?](http://stackoverflow.com/questions/7467335/does-javascript-equal-sign-reference-objects-or-clones-them) – RobG Sep 20 '11 at 06:14

4 Answers4

15

JavaScript Objects (and by extension: arrays, regexes, dates, non-primitive strings/numbers/booleans etc.) equality and assignment are reference based:

{a:'a'} == {a:'a'} // false

But:

var myObject = {a:'a'};
var myObject2 = myObject;

myObject == myObject2 // true

Furthermore:

myObject.b = 'b';

console.log(myObject2.b); // Logs: "b"
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
  • This is true, but doesn't really address the question above. This comparison is false because each of those are separate objects and equality for objects compares to see if they are the identically same object in memory - it doesn't look at the contents of the object. – jfriend00 Sep 20 '11 at 05:53
  • @jfriend00 - I don't understand what you're saying. I've clearly explained that both evaluation *and* assignment are reference based. – Joseph Silber Sep 20 '11 at 05:55
  • I only saw the first two lines when I read your post. I'm not sure if you edited your answer to add more or SO just didn't show me everything. Your current answer is more complete. – jfriend00 Sep 20 '11 at 05:57
5

Forget all the low-level nonsense about "references" (which do not exist in JavaScript) and consider the simple rules outlined below. The term "object" is used to represent a specific value, which is not necessarily an "Object"; this does not change the rules, but rather reinforces the consistency.

1) An object is itself. If an object is mutable and if that object is mutated then that object has been mutated.

2) An assignment does not create a copy/clone/duplicate of an object. This includes values "assigned" to the parameters in function calls. The object is the value.

3) A variable (or property) is not an object, rather it is a name for an object (a pretty way to say "the variable evaluates to a given object"). A single object can have many "names" and yet it is the same object.

Everything else is an implementation detail -- references are not talked about in the specifcation at all. While, it should be noted that the primitive values such as number and string cannot have additional properties assigned (the assignment is ignored) while the wrapper Objects Number and String are full-fledged objects. This is consistent with the above rules: there are no mutable non-Object values in JavaScript. For the sake of discussing the JavaScript language the primitives values can be thought of as objects although they are completely immutable and are not really Objects.

Happy coding.

3

Javascript is based on reference semantics:

var a = [1,2,3];
var b = a;
a[0] = 42;
alert(b[0]); // will show 42
var c = a.slice(); // explicitly makes a copy
a[1] = 6502;
alert(c[1]); // will show 2, not 6502
6502
  • 112,025
  • 15
  • 165
  • 265
2

Assignment makes a copy of the value only if it's a primitive type (like Number, Boolean, etc...). Otherwise, assignment just copies a reference to the same object (Object, Array, etc...). A new object is not created with assignment.

So, in your example:

obj1.list =new array();
// ------ populate list
obj2.list = obj1.list

obj2.push("foo");
alert(obj1.pop);    // "foo" (they are both the same list)

obj1.list and obj2.list will be pointing to the same object (contain references to the same object)

jfriend00
  • 683,504
  • 96
  • 985
  • 979