Whether a variable in JavaScript is a value or a pass

Source: Internet
Author: User
Tags data structures shallow copy

But it is the key to understanding data structures. The 4 terms in the title, corresponding in English are: shallow copy (note, not shadow copy), deep copy, pass by value, pass by reference (or pass by address). It's one thing to pass and pass references.

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 are you so divided? This involves memory allocation issues. For mutable types, you can theoretically allocate space with no size limits, as long as you allocate a limited amount of memory space, and for mutable types. Therefore, this is due to the rational use of system resources considerations. In fact, heap memory and stack memory are used to hold immutable type and variable type values, respectively.

What is an immutable type? That is, once the value is assigned to a variable, it belongs to only one variable and cannot belong to another variable. Such as:
Copy code code as follows:
Window.onload=function ()
{

var stringvalue = www.111cn.net;
var anotherstringvalue = stringvalue;
StringValue = "I have changed";
alert (stringvalue);//output I have changed
alert (anotherstringvalue);//Output faint


At this point, will the value saved in Anotherstringvalue also become "I have changed"? No. Because

var anotherstringvalue = stringvalue; Www.111cn.net

Copy a string (accordingly, allocate a new space in memory) and assign the string to Anotherstringvalue, as is the string stored in StringValue. In other words, although the two variables hold the same value, their values are not in one piece of memory. Therefore, modifying any one of the variables does not affect the other variable. That

StringValue = "I have changed";

Affects only the value of the stringvalue. But, to be exact, stringvalue = "I have changed"; instead of modifying stringvalue, you create a new string (which, in turn, allocates a new space in memory), and then let StringValue refer to the string-- More like the value of a replacement variable; The original string? Because there is no variable to reference it, it becomes garbage (of course, the memory consumed by the garbage will be recycled).

Thus, the assignment operation for the invariant type, passing is in the memory of the value itself. So, what about mutable types? Of course, a reference to the value in memory (or an address) is passed, and no matter how many times it is passed, there is always only one original value in memory-after all, the variable type size is unpredictable, and saving only one copy of the original value can save the maximum memory space. For example:
Copy code code 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

}

It is self-evident that the anotherobjectvalue here are derived from Objectvalue only to the original object ({1:1, ' s ': ' string ', ' InnerObject ': {' Innerarray '): [1,2,3] ), that is, the address of the object in memory, or "house number." Therefore, by objectvalue the first element of the original object (objectvalue[1] = 100;), the result is also reflected in anotherobjectvalue[1]-because the two variables share the same original value.

In JavaScript, passing arguments to a function is done according to the default convention-that is, the immutable type, the value, the variable type, the address. Such as:

function Example (str, obj) {
......
}
Example (Stringvalue,objectvalue);

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

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.