value type reference type object in javascript

前端 未结 4 765
悲&欢浪女
悲&欢浪女 2020-12-10 17:05

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

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

相关标签:
4条回答
  • 2020-12-10 17:20

    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)

    0 讨论(0)
  • 2020-12-10 17:24

    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.

    0 讨论(0)
  • 2020-12-10 17:26

    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"
    
    0 讨论(0)
  • 2020-12-10 17:29

    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
    
    0 讨论(0)
提交回复
热议问题