In JS, types are divided into basic types and reference types. The primitive type copy variable is called value pass, and the reference type copy variable is called reference pass.
1. Basic type replication
var num = 10 var numCopy = num console.log(numCopy); // 10 console.log(num); //10 num = 20 console.log(numCopy); // 10 console.log(num); / / 20Copy the code
As you can see in the code, num and numCopy are mutually exclusive. In primitive type copying, when the copied object copies the copied object, the copied object itself creates a new value, and then assigns the copied value to the position of the new variable. That is, when NumCopy copies num, numCpoy creates a new value. The value of num is then copied to the position numCopy newly assigned.
2. Reference type replication
Var object = {name:'hsy', age: 22} var obj = object console.log('obj',obj); //{name: "hsy", age: 22} console.log('object',object); //{name: "hsy", age: 22} obj.name ='wyx' console.log('object',object); //{name: "wyx", age: 22} console.log('obj',obj); //{name: "wyx", age: 22}Copy the code
As you can see from the code, object and obj affect each other. In reference types, one variable copies the other variable, and the value is copied to the newly allocated location, but essentially the pointer points to the same pointer.
3. Parameter transfer
In JS, in ES, all arguments are passed by value. But the puzzle is why there is value passing and reference passing when accessing variables, but all value passing in parameter passing. In effect, when you pass a value of the primitive type to a parameter, the passed value is copied to a local variable. When a reference type is passed, the memory address of the passed value is copied to a local variable, so changes to the local variable are reflected outside the function.
3.1 Value type parameter transfer
Function sum(num) {num += 10 return num} const count = 10 console.log(sum(count)); // 10 console.log(count) // 10Copy the code
Count does not change the data as num changes.
3.2 Reference type Parameter passing
function setName (obj) {
obj.name = 'hsy'
}
var p = new Object()
console.log(p); // {}
setName(p)
console.log(p); // {name: hsy}
Copy the code
The above code creates an object and stores the variable in p. This object is then passed to the setName() function and copied to obj. Inside the function, obj references the same object as P, so when the name attribute is added to obj inside the function, the external P will react. There is a misconception that the global scope is changed after the local scope is changed, which means that the parameter is passed by reference type. To prove this wrong, I have made a small change:
function setDoubleName (obj) {
obj.name ='wyx'
obj = new Object()
obj.name = 'xtt'
}
let p1 = new Object()
console.log(p1); // {}
setDoubleName(p1) //
console.log('p1',p1); //p1{name: "wyx"}
Copy the code
{name:”wyx”}, {name:”wyx”}, {name:”wyx”}, {name:”wyx”}, {name:”wyx”}, {name:”wyx”}, {name:”wyx”}, {name:”wyx”}, {name:”wyx”}, {name:”wyx”}, {name:”wyx”} But the original reference remains unread. In fact, when you override obj inside a function, the variable refers to a local variable that is destroyed immediately after the function is executed.
Here we can think of the parameters of the ES function as local variables