JavaScript_note 5 basic type values and reference type values (save, copy, pass by value)

JavaScript_note 5 basic type values ​​and reference type values ​​(save, copy, pass by value)

Save

There are five basic data types: Undefined, Null, Boolean, Number, and String, which access basic type values ​​by value; the value of reference type is stored in the object Object and is accessed by reference.
For the value of a reference type, you can add attributes and methods to it, or you can change or delete its attributes and methods.

var person = new Object();
person.name = 'Kabukiyo';
alert(person.name);

The var operator creates a variable person and saves a value, which is a reference type value and saves an object.

copy

Copy of basic type values

var num1 = 5;
var num2 = num1;

Both num1 and num2 hold the value 5, but these two values ​​are completely independent and do not affect each other.

Copy of reference type values

var person1 = new Object();
var person2 = person1;
person2.name = 'kabukiyo';
alert(person1.name); //'kabukiyo'

var defines two variables person1 and person2. Create an object and save it in the variable person1, and then copy person2 with person1.
So that person1 and person2 both save this object, that is, both variables refer to the same object.

transfer

The parameters of all ECMAScript functions are passed by value . That is, the value outside the function is passed to the inside of the function, which is directly copied to the internal parameters of the function.
The difference between function parameter passing:

  1. Pass by value: changing the passed value in the function will not affect the outside
  2. Pass by reference: changing the passed value inside the function will affect the outside

Example one,

var num1 = 1;
function change(num){
    
    
	console.log(num);  //1
	num=2;
	console.log(num);  //2
}
change(num1);
console.log(num1); // 1 

This example illustrates:

  1. The actual parameter num1 is passed to the formal parameter num, so the third line outputs 1 which is equivalent to copying
  2. The fourth line changes the value of num, but does not affect the value of num1
  3. Pass by value, changing the passed value inside the function will not affect the outside

Example two,

        function test(obj){
    
    
            alert(obj.name);  // kabukiyo
        }
        var person = new Object();
        person.name = 'kabukiyo';
        test(person);

The above example: var defines a variable person and saves an object, adds an attribute name to person, and assigns the attribute value to'kabukiyo'. Then person is passed as a parameter to the function, and obj and person refer to the same object.

        function test(obj){
    
    
            obj.name = 'Lin';
            // alert(obj.name); //Lin
        }
        var person = new Object();
        person.name = 'kabukiyo';
        test(person);
        //alert(person.name);//外部也变成了Lin

Since obj and person refer to the same object, the internal modification of the attribute name can also be reflected externally. But it is still not passed by reference.

        function test(obj){
    
    
            obj.name = 'Lin';
            obj = new Object();
            obj.name = 'ahua';
            // alert(obj.name); // ahua
        }
        var person = new Object();
        person.name = 'kabukiyo'; 
        test(person);
        alert(person.name)  //Lin。并没有变成ahua

If it is passed by reference, the execution of the statement obj = new Object();person should also be affected. But it didn't.
Also like

function example(num){
    
    
	alert(num);  // 1
	num = 2      // 改变了num
}
var num1 = 1;
example(num1);
alert(num1);    //还是1,不会因为 num = 2;而num1 变成了2;

Guess you like

Origin blog.csdn.net/qq_43263320/article/details/113566431