Arslan Ahson
Arslan Ahson

Reputation: 256

value type reference type object in javascript

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?

Upvotes: 7

Views: 8451

Answers (4)

jfriend00
jfriend00

Reputation: 707328

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)

Upvotes: 2

user166390
user166390

Reputation:

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.

Upvotes: 5

Joseph Silber
Joseph Silber

Reputation: 219938

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"

Upvotes: 15

6502
6502

Reputation: 114481

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

Upvotes: 3

Related Questions