Raw values and reference values in JavaScript are passed

Source: Internet
Author: User

A different memory allocations when declaring variables:

1) Raw values: simple data segments stored in stacks (stack), that is, their values are stored directly in the location of the variable access.

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.

2) 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.

b Different memory allocation mechanisms also bring different access mechanisms

1) in JavaScript, direct access to objects stored in heap memory is not allowed, so when an object is accessed,

The first thing to get is 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.

2) and the value of the primitive type is directly accessible.

c different when copying variables

1) 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.

2) 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.

This means that both of these 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). One more pointer.

The d parameter is passed differently (the process of copying the argument to the parameter)

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 values of primitive types and reference types? It's not just the differences in memory allocations.

1) Original value: Just pass the value in the variable to the parameter, and then the parameter and the variable do not affect each other.

2) 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.

Raw values and reference values in JavaScript are passed

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.