admin管理员组

文章数量:1252793

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?

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?

Share Improve this question edited Mar 26, 2013 at 22:28 Matt W-D 1,6252 gold badges19 silver badges22 bronze badges asked Sep 20, 2011 at 5:45 Arslan AhsonArslan Ahson 2561 gold badge3 silver badges10 bronze badges 1
  • 1 possible duplicate of Does Javascript equal sign reference objects or clones them? – RobG Commented Sep 20, 2011 at 6:14
Add a ment  | 

4 Answers 4

Reset to default 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"

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 pletely immutable and are not really Objects.

Happy coding.

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

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)

本文标签: value type reference type object in javascriptStack Overflow