Passing JavaScript function parameters

js function parameters are passed by value.

The value of the primitive type is passed the value of the variable

Reference types pass memory addresses

Examples are as follows:


var num = 10,
    name = "Addy Osmani",
    obj1 = {
      value: "first value"
    },
    obj2 = {
     value: "second value"
    },
    obj3 = obj2;
 
function change(num, name, obj1, obj2) {
    num = num * 10;
    name = "Paul Irish";
    obj1 = obj2;
    obj2.value = "new value"; //Change the attribute value of the variable pointed to by the memory address
}
 
change(num, name, obj1, obj2);
 
 console.log(num); // 10
 console.log(name);// "Addy Osmani"
 console.log(obj1.value);//"first value"
 console.log(obj2.value);//"new value"
 console.log(obj3.value);//"new value"  


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325795888&siteId=291194637