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
4 Answers
Reset to default 15JavaScript 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
版权声明:本文标题:value type reference type object in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740710103a2279619.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论