Home  >  Article  >  Web Front-end  >  A detailed introduction to passing by value in JavaScript

A detailed introduction to passing by value in JavaScript

不言
不言forward
2018-10-24 11:48:322399browse
This article brings you a detailed introduction to passing by value in JavaScript. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Overview

The passing of parameters is divided into passing by value and passing by reference, while the passing of parameters in JavaScript is only passed by value.

The parameters of all functions in ECMAScript are passed by value.

The so-called passing by value is:

Copying the value outside the function to the parameter inside the function is the same as copying the value from one variable to another variable. -- "JavaScript Advanced Programming"

We know that in JS, there are both basic data types and reference data types, so what is the difference between the two in passing by value?

Let me conclude first

When passing basic data types to parameters, the passed value will be copied to a local variable (an element in the arguments class array object). When passing a reference data type to a parameter, the memory address of the value is assigned to a local variable.

1. Data types

In JS, data types are divided into basic types and reference types.

The basic types include: number, string, boolean, undefined, null, Symbol (new in es 6). Values ​​of basic types are stored in stack memory.

The value of the basic data type itself will not change.

let num1 = 5;
let num2 = num1;

A detailed introduction to passing by value in JavaScript

After assigning the variable num1 that holds the original value to num2, a copy of the original value num1 will be assigned to the new variable num2. After this The two variables are completely independent. They just have the same value. They are completely independent copies and do not interfere with each other.

Reference data types include: Function, Array, Object, etc. in addition to basic data types. Reference data types are stored in heap memory.

JS does not allow direct manipulation of the memory space of the object, so the reference data type is accessed through the value stored in the variable, that is, a pointer (point), pointing to the memory address of the stored object.

let obj1 = new Object();
var obj2 = obj1;

A detailed introduction to passing by value in JavaScript

When the reference type variable obj1 is assigned to another variable obj2, obj2 actually accepts the memory address pointer of the reference type data. Therefore, to determine whether two reference types are equal, what is actually compared is whether the memory addresses are equal.

2. Pass by value

var num = 1;
function foo(param) {
    param = 2;
}
foo(num);
console.log(num); // num 值仍为1, 并没有受 param = 2 赋值影响

The above code:

Pass by value Each time a parameter is passed, a copy will be copied to the inside of the function, and the two before and after copies will be copied. The values ​​do not affect each other.

2. "Pass by reference"

var obj = {
    num: 1
};

function foo(o) {
    o.num = 2;
    console.log(obj.num);  // 2
}

foo(obj);
console.log(obj.num); // 2

In the above code, the foo function takes the obj object as an actual parameter. After execution, the num attribute of the obj object is changed, indicating that the parameter o object and The external variable obj object is the same object. Even though we agreed to pass by value, why did we still change the original object?

3. Pass by sharing

Look at the following code:

var obj = {
    num: 1
};

function foo(o) {
    o = 100;
}

foo(obj);
console.log(obj.num);  // 1

If it is passed by reference, it stands to reason that the obj object will be changed to 100. .

To be precise, the basic types in JS are passed by value, and the object types are passed by sharing (call by sharing, also called passing by object, passing by object sharing)

In the shared transfer, the function is The assignment of formal parameters does not affect the value of the actual parameter itself.

So, the object referenced by the formal parameter is the same. Since the object is mutable, modifying the attribute value of the object in the formal parameter will affect the attribute value of the original object.

Passing by reference is to pass a reference to the object, while passing by sharing is a copy of the copy of the object, so the copy itself cannot be modified directly. The copy is also a copy, so it is also considered to be passed by value.

The basic type itself is passed by value and is immutable. Modifications to the basic type essentially create new values ​​in the stack memory.

Review and consolidation:

var obj = { num : 0 };
obj.num = 100;
var o = obj;
o.num = 1;
obj.num; // 1, 被修改
o = true;
obj.num; // 1,  o 是对象的一个拷贝,对 o 本身的修改,不会改变 obj 对象本身的值。

Summary

Passing parameters in JavaScriptOnly pass by value, but for reference types Transfer is a kind of shared transfer. What is transferred is a copy of the data type. Although it refers to the same object, the actual parameter itself cannot be changed by changing the formal parameter.

This kind of copy is also considered to be passed by value in JS.

The above is the detailed content of A detailed introduction to passing by value in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete