1. Basic data type pass parameters:
funciton addTen(num){
num+=10;
return num;
}
var count=20;
var result=addTen(count);
alert(count);//20
alert(resullt);//30
The execution results are: 20 and 30. In this code, the variable count is passed as a parameter to the function addTen, which is equivalent to copying the value of the variable count to the parameter of the function addTen. At this time, the parameter num of addTen can be regarded as a variable inside the function. In the previous code, it is equivalent to copying the value between two basic data type variables. The basic data types have their own independent memory addresses, so num and count have nothing to do with each other. They are just equal values. After the function is executed, the value of count has not changed. The result outside the function is directly assigned, so the value of result is the result of the function 30.
2. Pass parameters to reference type:
function setName(obj){
="LSN";
}
var person=new Object();
setName(person);
alert();//LSN
The execution result is: LSN. In this code, the function setName is to add an attribute name to the obj object and assign the attribute to "LSN". Because obj is a reference type, it is to assign the reference type person to obj, that is, person and obj refer to a memory address. Therefore, when a new attribute name is added to obj, the person outside the function also changes, and the final result is LSN.
3. Is the parameter passing a reference type value or a reference?
function setName(obj){
="ABC";
obj=new Object();
="BCD";
}
var person=new Object();
setName(person);
alert();// ABC
The execution result is: ABC. The difference between Example 3 and Example 2 is that 2 lines of code are added to the function. After adding a new attribute name to the obj object and assigning a value, obj is defined as a new object (new Object()). After defining the new object, the name is assigned to the name and the new value "BCD". At this time, if it is passed by reference, the person object will be automatically modified to a new object whose name attribute is "BCD", but the last one is displayed is "ABC", which means that even if the value of the parameter is modified inside the function, the original reference remains unchanged. In fact, when obj=new Object() inside the function, this new obj has become a local object inside the function, and this object will be automatically destroyed after the function is executed.
Copy the codeThe code is as follows:
funciton addTen(num){
num+=10;
return num;
}
var count=20;
var result=addTen(count);
alert(count);//20
alert(resullt);//30
The execution results are: 20 and 30. In this code, the variable count is passed as a parameter to the function addTen, which is equivalent to copying the value of the variable count to the parameter of the function addTen. At this time, the parameter num of addTen can be regarded as a variable inside the function. In the previous code, it is equivalent to copying the value between two basic data type variables. The basic data types have their own independent memory addresses, so num and count have nothing to do with each other. They are just equal values. After the function is executed, the value of count has not changed. The result outside the function is directly assigned, so the value of result is the result of the function 30.
2. Pass parameters to reference type:
Copy the codeThe code is as follows:
function setName(obj){
="LSN";
}
var person=new Object();
setName(person);
alert();//LSN
The execution result is: LSN. In this code, the function setName is to add an attribute name to the obj object and assign the attribute to "LSN". Because obj is a reference type, it is to assign the reference type person to obj, that is, person and obj refer to a memory address. Therefore, when a new attribute name is added to obj, the person outside the function also changes, and the final result is LSN.
3. Is the parameter passing a reference type value or a reference?
Copy the codeThe code is as follows:
function setName(obj){
="ABC";
obj=new Object();
="BCD";
}
var person=new Object();
setName(person);
alert();// ABC
The execution result is: ABC. The difference between Example 3 and Example 2 is that 2 lines of code are added to the function. After adding a new attribute name to the obj object and assigning a value, obj is defined as a new object (new Object()). After defining the new object, the name is assigned to the name and the new value "BCD". At this time, if it is passed by reference, the person object will be automatically modified to a new object whose name attribute is "BCD", but the last one is displayed is "ABC", which means that even if the value of the parameter is modified inside the function, the original reference remains unchanged. In fact, when obj=new Object() inside the function, this new obj has become a local object inside the function, and this object will be automatically destroyed after the function is executed.