Introduction to reference examples in Javascript
In Javascript scripts, the referenced parameter can be modified internally, but the reference corresponding to the parameter cannot be modified. The following is a detailed description.
In Javascript scripts, the parameter reference principle is that the referenced parameter can be modified internally (such as attribute), but the reference corresponding to the parameter cannot be modified.
A test example is as follows:
The Code is as follows:
<Script language = "javascript">
// Dosomething1. for reference, the variable itself cannot be modified, but the internal structure of the variable can be modified.
Function dosomething1 (){
A = 'try ';
}
// Test 1
Function test1 (){
Var a = {a: 'test', B: 'Is ', c:' OK '};
Dosomething1 ();
Alert (a. );
}
// Dosomething2
Function dosomething2 (v ){
V. a = v. a + '!!! '; // Modify the attributes of the referenced variable. The modification is successful.
V = 'try'; // An error occurred while trying to modify the variable reference.
}
// Test 2
Function test2 (){
Var a = {a: 'test', B: 'Is ', c:' OK '};
Dosomething2 ();
Alert (a. );
}
Test2 ();
</Script>