Access to values in JavaScript and parameter passing issues

Source: Internet
Author: User
Tags call by reference

Data type

The data types in JavaScript can be divided into two categories:

    • The original data type value primitive type, such as undefined,null,boolean,number,string.
    • A reference type value , that is, an object type, objects, such as Object,array,function,date.
Different memory allocations when declaring variables
    • Raw values: Simple data segments stored in stacks (stack), that is, their values are stored directly in the location where the variable is accessed . This is because the space occupied by these primitive types is fixed, so they can be stored in a smaller memory area – the stack. This stores the values that facilitate quick lookup of variables.
    • Reference value: The object stored in the heap (heap), that is, the value stored at the variable is a pointer (point) to the memory address of the storage object. This is because the size of the reference value will change, so it cannot be placed on the stack, otherwise it will reduce the speed of the variable search. Instead, the value placed in the stack space of the variable is the address that the object stores in the heap. The size of the address is fixed, so storing it in the stack has no negative effect on the performance of the variable.

Different memory allocation mechanisms also bring different access mechanisms

In JavaScript, it is not allowed to directly access objects stored in the heap memory, so when accessing an object, we first get the address of the object in the heap memory, and then follow this address to get the value of this object, which is the legendary access by reference . The value of the primitive type is directly accessible.

Different when copying variables
    • Original value: When you copy a variable that holds the original value to another variable, the copy of the original value is assigned to the new variable, and then the two variables are completely independent, they just have the same value.

    • Reference value: When you copy a variable that holds an object's memory address to another variable, the memory address is assigned to the new variable, which means that the two variables point to the same object in the heap memory, and any change they make will be reflected on the other. (one thing to understand here is that copying an object does not make an identical object in the heap memory, just a variable that holds pointers to the object)

Differences in Parameter passing

First we should make it clear that the parameters of all functions in ECMAScript are passed by value . But why is there still a difference when it comes to the value of the original type and the reference type, not just because of the difference in memory allocation. (I've compared this to the exact same mechanism that is used to copy variables, and you can simply understand that when you pass a parameter, it's the process of copying the argument to the formal parameter.)

    • Original value: Simply pass the value in the variable to the parameter, and then the parameter and the variable do not affect each other.
    • Reference value: Object variable The value inside it is the memory address of the object in the heap memory, which you should always keep in mind! So the value that it passes is the memory address, which is why the changes to this parameter inside the function are external, because they all point to the same object. Maybe after I say this, you still have a little bit of understanding of the examples in the book, so please take a look at the picture:

So, if it is passed by reference, the contents of the second lattice (i.e. the variable itself) are passed in (there will be no fourth lattice). But the fact is that the variable passes the value inside it (copied) to the parameter, so that the parameter also points to the original object. So if you assign this parameter to another object inside the function, this parameter will change its value to point to the new object's memory address, but at this point the original variable still points to the original object, when they are independent of each other, but if this parameter is to change the properties inside the object, this change will be reflected in the external , because the object they are pointing to has been modified! Let's take a look at the following example: (Legendary call by sharing)

123456789101112131415161718192021222324 var obj1 = { value:' 111 ' }; var obj2 = { value:' 222 ' };function changestuff(obj){ obj. Value = ' 333 '; obj = obj2; return obj. Value; } var foo = changestuff(obj1); console. Log(foo); The ' 222 ' parameter, obj, points to the new object Obj2 console. Log(obj1. Value); / * ' 333 ' obj1 still points to the original object, the reason value changed,* Because the first statement in Changestuff, this time obj is pointing to obj1.* Again, if it is passed by reference, this time obj1.value should be equal to ' 222 '*/

All right, here's the whole explanation of the problem.

If you have any interest, you can get to know the call by value, the mechanism of call by reference call by sharing, call by sharing

and StackOverflow. The problem of function passing is very well explained and worth seeing. (There are links below)

Reference:

    • ECMAScript raw values and reference values
    • Is JavaScript a pass-by-reference or Pass-by-value language?
    • My red Book, "JavaScript Advanced Programming" p69-p71

Reprinted from: Fehacker

Access to values in JavaScript and parameter passing issues

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.