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