Is the variable in Javascript a value or an address?

Source: Internet
Author: User

This title is easy to read, but it is the key to understanding the data structure. The four terms in the title correspond to shallow copy (note, not shadow copy), deep copy, pass by value, pass by reference (or pass by address ). It is one thing to transfer an address and a reference.

One doorProgramming LanguageThe core of the data structure is the data structure. In a rough sense, the data structure can be divided into immutable and mutable ). Why? This involves memory allocation. For immutable types, you only need to allocate a limited amount of memory space. For immutable types, theoretically you need to allocate space without the size limit. Therefore, this score is based on the rational use of system resources. In fact, heap memory and stack memory are used to save immutable type values and variable type values respectively.

What is an immutable type? That is, once the value is assigned to a variable, it only belongs to a variable and cannot belong to other variables. For example:CopyCodeThe Code is as follows: window. onload = function ()
{

VaR stringvalue = "Faint ";
VaR anotherstringvalue = stringvalue;
Stringvalue = "I have changed ";
Alert (stringvalue); // output I have changed
Alert (anotherstringvalue); // output a faint margin

At this time, will the saved value in anotherstringvalue also change to "I have changed "? No. Because

VaR anotherstringvalue = stringvalue;

Copy a string (correspondingly, allocate a new space in memory) as the string saved in stringvalue, and assign the string to anotherstringvalue. In other words, although these two variables Save the same value, their values are not in a memory. Therefore, modifying any variable does not affect another variable. That is

Stringvalue = "I have changed ";

Only the stringvalue value is affected. However, to be exact, stringvalue = "I have changed"; instead of modifying stringvalue, it creates a new string (correspondingly, allocating a new space in the memory ), then let stringvalue reference this string-more like replacing the value of the variable; what about the original string? Because no variable references it, it becomes garbage (of course, the memory occupied by garbage will be recycled ).

It can be seen that the value assignment operation transmits the value itself in the memory for the unchanged type. So what about variable types? Of course, the reference (or address) of the memory value is passed, and no matter how many times it is passed, there is always only one original value in the memory-after all, the variable type size is unpredictable, saving only one original value can save the maximum memory space. For example:Copy codeThe Code is as follows: window. onload = function ()
{
VaR objectvalue = {,'s ': 'string', 'innerobject': {'innerarray': [, 3]};
VaR anotherobjectvalue = objectvalue;
Objectvalue: [1] = 100;
Alert (anotherobjectvalue [1]); // output 100

}

It is self-evident that the anotherobjectvalue here only obtains the original object ({,'s ': 'string', 'innerobject': {'innerarray ': [1, 2, 3]}), that is, the address of the object in the memory, or the "house number ". Therefore, use objectvalue to modify the first element of the original object (objectvalue [1] = 100 ;), the results will also be reflected in anotherobjectvalue [1]-because the two variables share the same original value.

In JavaScript, passing parameters to a function is performed according to the above default convention-that is, for an immutable type, passing a value; for a mutable type, passing an address. For example:

Function example (STR, OBJ ){
......
}
Example (stringvalue, objectvalue );

When the example function is called, the first parameter passes the actual string value, and the second parameter passes the object reference (memory address ).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.