Home >Web Front-end >JS Tutorial >Learn Js from scratch (detailed analysis of parameter passing of basic data types and reference types)_Basic knowledge

Learn Js from scratch (detailed analysis of parameter passing of basic data types and reference types)_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 17:56:071069browse

1. Pass parameters of basic data types:

Copy code The code is as follows:

 funciton addTen(num ){
 num =10;
 return num;
 }
 var count=20;
 var result=addTen(count); 🎜> alert(result);//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 above code, it is equivalent to copying values ​​between two basic data type variables. The basic data types have their own independent memory addresses, so num and count have no relationship. They are just equal in value. After the function is executed, the value of count does not change. The result outside the function is assigned directly, so the value of result is the result of the function 30.
 2. Pass parameters by reference type:


Function setName (obj){
 obj.name="LSN";
 }
var person=new Object();
setName(person);
alert(person.name);// LSN


The execution result is: LSN. In this code, the function setName adds an attribute name to the obj object and assigns the attribute "LSN". Because obj is a reference type, the reference type person is assigned to obj, that is, It is said that person and obj refer to a memory address, so when a new attribute name is added to obj, the person outside the function also changes, and the final result of person.naem is LSN.
 3. Is the reference type parameter passed a value or a reference?


function setName(obj){
obj.name="ABC";
obj=new Object();
obj.name="BCD";
}
var person=new Object();
setName(person);
alert(person.name);// ABC


The execution result is: ABC. The difference between Example 3 and Example 2 is that two more 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()), and the new object is defined. Then the name is assigned a new value "BCD". If it is passed by reference at this time, then the final person object will be automatically modified to point to a new object whose name attribute is "BCD", but the final display will be "ABC", which shows that even if the parameters are modified inside the function value, but the original reference remains unchanged. In fact, when obj=new Object() inside the function, the new obj has become a local object inside the function. This object will be automatically destroyed after the function is executed.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn