first quote original
Whencalls a function, the value passed to the function is called the function argument (value passing), and the function parameter at the corresponding position is called the formal parameter. If the argument is a variable that contains the original value (number, string, Boolean), even if the function changes the value of the corresponding parameter internally, the value of the argument will not change when it is returned. If the argument is an object reference, the corresponding parameter points to the same object as the argument. If the function changes the value of the corresponding parameter internally, the value of the object pointed to by the argument will also change when returned:
to verify, I wrote the following code:
var x=1,y=2; // x, y
var i=new String("1"), j = new String("2"); // i,j
console.log("x="+x+",y="+y);
function fnTest(a,b){
a=a*2;
b=b*4;
return a+b;
}
console.log("function result="+ fnTest(x,y));
console.log("x="+x+",y="+y);
console.log("typeof x="+typeof(x) + ",typeof y="+typeof(y));
console.log("function result="+ fnTest(i,j));
console.log("i="+i+",j="+j);
console.log("typeof i="+typeof(i) + ",typeof j="+typeof(j));
output:
x=1,y=2
myJavaScript.js:28 typeof x=number,typeof y=number
myJavaScript.js:29 i=1,j=2
myJavaScript.js:30 typeof i=object,typeof j=object
myJavaScript.js:36 function result=10
myJavaScript.js:37 x=1,y=2
myJavaScript.js:38 typeof x=number,typeof y=number
myJavaScript.js:39 function result=10
myJavaScript.js:40 i=1,j=2
myJavaScript.js:41 typeof i=object,typeof j=object
you can see that the value of iMagnej, which stores the reference type, has not changed. Why? Is there anything wrong with the original text?