Examples are referenced in Javascript

  • 2020-03-30 02:04:00
  • OfStack

In Javascript scripts, the reference principle for parameters: referenced parameters (such as properties) can be modified internally, but the reference for the parameter cannot be modified.

A test example is as follows:
 
<script language="javascript"> 
//Dosomething1, the variable itself cannot be modified for reference, but the internal structure of the variable can be modified
function dosomething1(a){ 
a = 'try'; 
} 

//Test 1
function test1(){ 
var a = {a:'test',b:'is',c:'ok'}; 
dosomething1(a); 
alert(a.a); 
} 
//dosomething2 
function dosomething2(v){ 
v.a = v.a + '!!!'; //Modify the properties of the reference variable
v = 'try'; //An attempt to modify the variable reference failed
} 
//Test 2
function test2(a){ 
var a = {a:'test',b:'is',c:'ok'}; 
dosomething2(a); 
alert(a.a); 
} 
test2(); 
</script> 

Related articles: