Home > Web Front-end > JS Tutorial > body text

Are variables in JavaScript passed by value or by address? _javascript skills

WBOY
Release: 2016-05-16 18:29:08
Original
1286 people have browsed it

This title is a bit confusing to pronounce, but it is the key to understanding data structures. The corresponding English terms for the four terms in the title are: shallow copy (note, not shadow copy), deep copy, pass by value, pass by reference (or pass by address). Passing by address and passing by reference are the same thing.

The core of a programming language is the data structure. Roughly speaking, the data structure can be divided into immutable types (immutable) and mutable types (mutable). Why is it so divided? This involves memory allocation issues. For immutable types, you only need to allocate limited memory space, while for immutable types, theoretically, you need to allocate space with no size limit. Therefore, this classification is based on the rational use of system resources. In fact, heap memory and stack memory are used to store immutable type values ​​and mutable type values ​​respectively.

What are immutable types? That is, once the value is assigned to a variable, it only belongs to that variable and cannot belong to other variables. For example:

Copy code The code is as follows:

window.onload=function()
{

var stringValue = "light";
var anotherStringValue = stringValue;
stringValue = "I have changed";
alert(stringValue);// Output I have changed
alert(anotherStringValue);//Output a faint


At this time, will the value saved in anotherStringValue also become "I have changed"? Won't. Because

var anotherStringValue = stringValue;

copies a string as it is in the string saved in stringValue (correspondingly, allocates a new space in memory), and replaces the string Assigned to anotherStringValue. In other words, although these two variables hold the same value, their values ​​are not in the same memory. Therefore, modifying any variable will not affect the other variable. That is,

stringValue = “I have changed”;

will only affect the value of stringValue. However, to be precise, stringValue = "I have changed"; does not modify stringValue, but creates a new string (correspondingly, allocates a new space in memory), and then lets stringValue refer to the string - more Like replacing the value of a variable; what about the original string? Because no variables refer to it, it becomes garbage (of course, the memory occupied by garbage will be recycled).

It can be seen that for immutable types, the assignment operation transfers the value itself in the memory. So, what about mutable types? Of course, what is passed is the reference (or address) of the value in the memory, and no matter how many times it is passed, there is always only one copy of the original value in the memory - after all, the size of the variable type is unpredictable, and saving only one copy of the original value can maximize the Save memory space. For example:
Copy code The code is as follows:

window.onload=function()
{
var objectValue = {1:1,'s':'string','innerObject':{'innerArray' : [1,2,3]}};
var anotherObjectValue = objectValue;
objectValue[1] = 100;
alert(anotherObjectValue[1]); //Output 100

}

Self-explanatory, anotherObjectValue here is obtained from objectValue through assignment operation Only a reference to the original object ({1:1,'s':'string','innerObject':{'innerArray' : [1,2,3]}}) is obtained, that is, the object in memory Address, or "house number." Therefore, modifying the first element of the original object through objectValue (objectValue[1] = 100;) will also be reflected in anotherObjectValue[1] - because these two variables share the same original value.

In JavaScript, passing parameters to functions follows the above default convention—that is, for immutable types, pass by value; for variable types, pass by address. For example:

function example(str, obj){
......
}
example(stringValue,objectValue);

When calling the example function, the first parameter What is passed is the actual string value, and the second parameter is passed a reference to the object (memory address).
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template