Thinking caused by the assignment of the object often asked in the interview
1.
var a = 1;
var b = a;
a = 2;
console.log(b); // 1
var c = [1,2,3];
var d = c;
c.shift();
console.log(d); // [2,3]
We know that the assignment of the basic object is a copy value, so changing a does not affect b, while the complex object assignment copies the pointer, so changing c affects d
2.
but now there are some strange phenomena
var a = [1,2,3];
var b = a;
a = [1,2,3,4];
console.log(b); // [1,2,3]
changed a but b didn"t change here. I don"t know what happened in the middle. Did I open up an address to store a [1mage2jin3jin4]? B or point to the previous a?
3.
var factorial = function() {
return 1
};
var trueFactorial = factorial;
factorial = function () {
return 2;
}
console.log(trueFactorial()); // 1
it"s strange, isn"t it the re-assignment of factorial? Isn"t the assignment of function a shallow copy?
4.
var factorial = function() {
return factorial();
};
var trueFactorial = factorial;
factorial = function () {
return 2;
}
console.log(trueFactorial()); // 2
Ah, the function that can be called normally to the second assignment here
2, a = [1 code > creates a new array object, and then points a
to it
3, var trueFactorial = factorial;
assigns the object pointed to by factorial
to trueFactorial
, so trueFactorial
points to the originally declared method
.
compound types (Function, Object, etc.) are assigned reference passing , which all point to the same memory address,
{
"type": "Program",
"start": 0,
"end": 41,
"body": [
{
"type": "VariableDeclaration",
"start": 0,
"end": 16,
"declarations": [
{
"type": "VariableDeclarator",
"start": 4,
"end": 15,
"id": { "type": "Identifier", "start": 4, "end": 5, "name": "a" },
"init": {
"type": "ArrayExpression",
"start": 8,
"end": 15,
"elements": [
{ "type": "Literal", "start": 9, "end": 10, "value": 1, "raw": "1" },
{ "type": "Literal", "start": 11, "end": 12, "value": 2, "raw": "2" },
{ "type": "Literal", "start": 13, "end": 14, "value": 3, "raw": "3" }
]
}
}
],
"kind": "var"
},
{
"type": "VariableDeclaration",
"start": 17,
"end": 27,
"declarations": [
{
"type": "VariableDeclarator",
"start": 21,
"end": 26,
"id": { "type": "Identifier", "start": 21, "end": 22, "name": "b" },
"init": { "type": "Identifier", "start": 25, "end": 26, "name": "a" }
}
],
"kind": "var"
},
{
"type": "ExpressionStatement",
"start": 28,
"end": 41,
"expression": {
"type": "AssignmentExpression",
"start": 28,
"end": 41,
"operator": "=",
"left": { "type": "Identifier", "start": 28, "end": 29, "name": "a" },
"right": {
"type": "ArrayExpression",
"start": 32,
"end": 41,
"elements": [
{ "type": "Literal", "start": 33, "end": 34, "value": 1, "raw": "1" },
{ "type": "Literal", "start": 35, "end": 36, "value": 2, "raw": "2" },
{ "type": "Literal", "start": 37, "end": 38, "value": 3, "raw": "3" },
{ "type": "Literal", "start": 39, "end": 40, "value": 4, "raw": "4" }
]
}
}
}
],
"sourceType": "script"
}
this is the program you want. Please distinguish what the assignment statement does. The assignment statement changes the value of Identifier. The pop statement is a member method and does not change the direction of the Identifer.