Elevation page 66 says that the function parameters num2 and argument [1] are different memory space, why my experiment does not support this conclusion.
function doadd (num1,num2){
arguments[1] = [1,2];
console.log(arguments[1]===num2)
}
first of all, let"s talk about why the two of them are different spaces, because if only one parameter is passed,
doadd (5)
, num2 is undefined,arguments [1] is [1], so it must be different spaces
.
however, if it is a different space, why does" doadd (5, [1jue 2]) "output true, that is, arguments [1] = num2, that is, they have the same pointer, that is, pointing to a space [1jue 2]?
add: we know that [1 num2 2] = = [1 false 2] = = > because their heap memory is different
now arguments [1] = true proves that they point to the same heap memory, so there is a problem with elevation
Is there any abstraction of memory model in
js, do not apply the concepts of static language to js
use memory model to help understand, do not mechanically copy it, which book do you mean by elevation?
Since
is an experiment, of course you need to give more examples, not to mention the special
of
[1jue 2]
.
function doadd (num1,num2){
arguments[1] = [1,2];
console.log(arguments[1], num2)
}
doadd(5,[1,2,3]); //[1, 2] [1, 2]
doadd(5,1); //[1, 2] [1, 2]
doadd(5); //[1, 2] undefined
if the number of arguments and formal parameters is equal, then there is a mapping relationship between formal parameters and argments: the arguments [I] where the values of the formal parameters change the corresponding position also change, and vice versa.
if there are fewer arguments than formal parameters, then there is no mapping between the excess formal parameters and argments. That is, the extra parameter is worth changing, and the value of the corresponding position of arguments [I] is still undefined, and vice versa.
< hr >
in addition, [1J 2]
is not equal to [1J 2]
. Your experiment is equal because num2's [1J 2]
is replaced by [1J 2]
of argments [1]
.
look at the following code:
let arr1 = [1, 2];
let arr2 = arr1;
console.log(arr1===arr2)//true
arr1 and arr2 refer to the same array in different memory spaces.
this should be the same as what you said. Pointers in
CPP, reference passing and value passing in JAVA are similar concepts.
borrows the concept of CPP pointer to describe it, it may be easier to understand, such as the following figure
arr1 memory is 0x2143123 in memory 0xa33234,
arr1 = = arr2 because their values are both 0x123412, but they are in different memory spaces.
just like num1=1,num2=1,
num1= num2//true.
the explanation may not be specially prepared, please point out any inaccuracies.