We all know that JavaScript contains basic and reference type values. I will share the applications of these two types in variable interaction. I have practiced and encountered problems ~~~~
1. Value of the value assignment variable:
[1]Basic Type Value
VaR num = 5;
VaR num1 = num;
The actual method is to copy a copy of num 5 and assign it to num1. Although they are equal, the values of the two variables are completely independent.
Verification:
Alert (num1); // 5
Alert (Num + 5); // 10
[2]Reference Type
When a variable is assigned a reference type to another variable, a copy is also copied to another variable, but here the pointer is copied, this pointer points to an object in the stack.
If one of the variables changes, the other also changes. See:
VaR obj1 = new object ();
VaR obj2 = obj1;
Obj1.name = "XXX ";
Alert (obj2.name); // "XXX"
2. parameter transfer
[1]The basic type value is passed by value.
If a variable transmits a value to a function parameter, the passed value is copied to the parameter (the arguments element of ecmascept ),
Function addnum (Num ){
Num = num + 10;
Return num;
}
VaR num = 10;
VaR result = addnum (Num );
Alert (Num); // 10 is not changed
Alert (result); // 20
[2] The reference type is still passed by value !!!!!
Function setname (OBJ ){
OBJ. Name = "XXX ";
}
VaR man = new object ();
Setname (OBJ );
Alert (man. Name); // "XXX"
HereNever think it is a reference TransferIf you think so, you have to look at the followingCode:
Function setname (OBJ ){
OBJ. Name = "XXX ";
OBJ = new object ();
OBJ. Name = "wrong ";
}
VaR man = new object ();
Setname (man );
Alert (man. Name); // or "XXX"
The example above demonstrates that if the value is passed by reference, the result should be "wrong ".
In fact, declaring an OBJ inside the function is only a local object, and the object will be destroyed after the function is executed.
Due to my limited level, I cannot cite some high-end examples. Sorry.
If you think there is something wrong or incomplete, please correct it and try again.