Is JS passed by value or by reference_javascript tips
Pass by value VS. Pass by reference
Call by value is the most commonly used evaluation strategy: the formal parameters of a function are copies of the actual parameters passed when called. Changing the value of the formal parameter does not affect the actual parameter.
When passing by reference (call by reference), the formal parameters of the function receive implicit references to the actual parameters, rather than copies. This means that if the values of function parameters are modified, the actual parameters will also be modified. At the same time both point to the same value.
Passing by reference makes it more difficult to trace function calls and sometimes causes subtle bugs.
Passing by value requires a clone every time, so the performance is lower for some complex types. Both methods of passing values have their own problems.
Let’s first look at a C example to understand the difference between passing by value and reference:
void Modify(int p, int * q)
{
p = 27; // Pass by value - p is a copy of the actual parameter a, only p is modified
*q = 27; // q is a reference to b, both q and b are modified
}
int main()
{
int a = 1;
int b = 1;
Modify(a, &b); // a is passed by value, b is passed by reference,
// a has not changed, b has changed
Return(0);
}
Here we can see:
a => When p is passed by value, modifying the value of formal parameter p does not affect the actual parameter a, which is just a copy of a.
b => q is passed by reference. Modifying the value of the formal parameter q also affects the value of the actual parameter b.
Explore how JS values are passed
The basic types of JS are passed by value.
var a = 1;
function foo(x) {
x = 2;
}
foo(a);
console.log(a); // Still 1, not affected by x = 2 assignment
Look at the object again:
var obj = {x : 1};
function foo(o) {
o.x = 3;
}
foo(obj);
console.log(obj.x); // 3, modified!
Explain that o and obj are the same object, and o is not a copy of obj. So it's not passed by value. But does this mean that JS objects are passed by reference? Let’s look at the following example:
var obj = {x : 1};
function foo(o) {
o = 100;
}
foo(obj);
console.log(obj.x); // Still 1, obj has not been modified to 100.
If it is passed by reference, modifying the value of formal parameter o should affect the actual parameter. But modifying the value of o here does not affect obj. Therefore objects in JS are not passed by reference. So how is the value of the object transferred in JS?
call by sharing
To be precise, basic types in JS are passed by value, and object types are passed by sharing (call by sharing, also called passing by object and passing by object sharing). It was first proposed by Barbara Liskov in the GLU language in 1974. This evaluation strategy is used in Python, Java, Ruby, JS and other languages.
The key point of this strategy is: when calling a function to pass parameters, the function accepts a copy of the object argument reference (neither a copy of the object passed by value, nor an implicit reference passed by reference). The difference between it and pass by reference is that the assignment of function parameters in shared transfer will not affect the value of the actual parameter. As in the following example, the value of obj cannot be modified by modifying the value of the formal parameter o.
var obj = {x : 1};
function foo(o) {
o = 100;
}
foo(obj);
console.log(obj.x); // Still 1, obj has not been modified to 100.
However, although the reference is a copy, the object referenced is the same. They share the same object, so modifying the property values of the formal parameter object will also affect the property values of the actual parameters.
var obj = {x : 1};
function foo(o) {
o.x = 3;
}
foo(obj);
console.log(obj.x); // 3, modified!
For object types, since the object is mutable, modifying the object itself will affect the references and reference copies that share the object. As for basic types, since they are all immutable, there is no difference between passing by sharing and passing by value (call by value). Therefore, JS basic types conform to both passing by value and passing by sharing.
var a = 1; // 1 is number type, immutable var b = a; b = 6;
According to the pass-by-share evaluation strategy, a and b are two different references (b is a reference copy of a), but refer to the same value. Since the basic type number 1 here is immutable, there is no difference between passing by value and passing by sharing.
The immutable nature of basic types
Basic types are immutable (immutable), only objects are mutable (mutable). For example, the numerical value 100, the Boolean value true, false, modifying these values (such as changing 1 to 3, changing true to 100) does not What's the meaning. What is easier to misunderstand is string in JS. Sometimes we try to "change" the contents of a string, but in JS, any operation that appears to "modify" a string value actually creates a new string value.
var str = "abc";
str[0]; // "a"
str[0] = "d";
str; // Still "abc"; assignment is invalid. There is no way to modify the content of the string
But objects are different, objects are mutable.
var obj = {x : 1};
obj.x = 100;
var o = obj;
o.x = 1;
obj.x; // 1, modified
o = true;
obj.x; // 1, will not change due to o = true
Define the variable obj here, the value is object, and then set the value of the obj.x attribute to 100. Then define another variable o, whose value is still the object object. At this time, the values of the two variables obj and o point to the same object (share a reference to the same object). Therefore, modifying the content of the object will affect both obj and o. But the object is not passed by reference. The value of o is modified by o = true, which will not affect obj.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use JS and Baidu Map to implement map pan function Baidu Map is a widely used map service platform, which is often used in web development to display geographical information, positioning and other functions. This article will introduce how to use JS and Baidu Map API to implement the map pan function, and provide specific code examples. 1. Preparation Before using Baidu Map API, you first need to apply for a developer account on Baidu Map Open Platform (http://lbsyun.baidu.com/) and create an application. Creation completed

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

Overview of how to use JS and Baidu Maps to implement map click event processing: In web development, it is often necessary to use map functions to display geographical location and geographical information. Click event processing on the map is a commonly used and important part of the map function. This article will introduce how to use JS and Baidu Map API to implement the click event processing function of the map, and give specific code examples. Steps: Import the API file of Baidu Map. First, import the file of Baidu Map API in the HTML file. This can be achieved through the following code:

How to use JS and Baidu Maps to implement the map heat map function Introduction: With the rapid development of the Internet and mobile devices, maps have become a common application scenario. As a visual display method, heat maps can help us understand the distribution of data more intuitively. This article will introduce how to use JS and Baidu Map API to implement the map heat map function, and provide specific code examples. Preparation work: Before starting, you need to prepare the following items: a Baidu developer account, create an application, and obtain the corresponding AP

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

How to use JS and Baidu Maps to implement map polygon drawing function. In modern web development, map applications have become one of the common functions. Drawing polygons on the map can help us mark specific areas for users to view and analyze. This article will introduce how to use JS and Baidu Map API to implement map polygon drawing function, and provide specific code examples. First, we need to introduce Baidu Map API. You can use the following code to import the JavaScript of Baidu Map API in an HTML file
