(1) Comparison of values--comparison of references
First, the comparison of the original values is the comparison of the values: they are equal only when their values are equal
Like a simple
var a1 = Ten; var a2 = Ten//true
Second, the comparison of objects is not a comparison of values: objects are compared by reference, and they are equal only if they refer to the same base object.
Even if two objects contain the same attributes and the same values, they are not equal. Two arrays that are exactly equal for each index element are not equal
Like what
var o = {x:1},p={x:1//false two separate objects unequal var a = [' you '],b = [' You '] c6>//False Two separate arrays are not equal
var a = []; var b = A; // b references the same array ab[0] = "You"; Console.log (a[// This is a also modified / / true
So, if we want to get a copy of an object or an array, or if we want to compare two individual objects or array equality, we should compare their attribute elements and so on, by iterating through the array to achieve
(2) Passing by value--by reference
Pass-by-value is the most commonly used evaluation strategy: a function's formal parameter is a copy of the argument that was passed when it was called. Modifying the value of a parameter does not affect the argument.
When passed by reference (call by reference), the function's formal parameter receives an implicit reference to the argument, not the copy. This means that if the value of a function parameter is modified, the argument is also modified. At the same time, they point to the same value
1. The basic type of JS is passed by value
Like what
var a = 1; function Foo (a) { = 2; Console.log (a); // This copy is 2 // is still 1, unaffected by a = 2 assignment
2. But what about objects or arrays? Let's look at an example.
Like what
var obj = {x:1}; function foo (o) { = 3; // 3, has been modified! // 3, has also been modified!
Description O and obj are the same object, and O is not a copy of obj. So it is not passed by value. But does this mean that JS objects are passed by reference? Let's look at the following example:
Like what
var obj = {x:1}; function foo (o) { =+/// is still 1, and obj has not been modified to.
If you are passing by reference, modifying the value of the parameter o should affect the argument. But modifying the value of O here does not affect obj.
Therefore, the object in JS is not passed by reference. So how does the value of the object in JS Pass?
Delivery by share call by sharing
To be precise, the basic type in JS is passed by value, and the object type is passed by share (call by sharing, also called by object, passed by object share)
the focus of this strategy is that when you invoke a function argument, the function accepts a copy of the object argument reference (neither a copy of the object passed by value nor an implicit reference passed by reference).
it differs from passing by reference in that the assignment of a function parameter in a shared pass does not affect the value of the argument. As in the above example, you cannot modify the value of obj by modifying the value of the parameter O.
However, although references are replicas, the referenced objects are the same. They share the same object, so modifying the property value o.x=3 of the Parameter object also affects the property value of the argument.
An additional example
var obj = {x:1= +; var o == 1// 1, modified true// 1, not due to o = True Change
For an object type, because the object is mutable (mutable), modifying the object itself affects the reference and reference replicas that share the object.
For basic types, because they are immutable (immutable), there is no difference between shared delivery and per-value delivery (call by values), so that the JS base type conforms to both by-value delivery and to shared-by-share delivery.
JavaScript passed by value & by reference