Differences between basic data types and reference data types in JavaScript

Source: Internet
Author: User

1. Basic data types and reference data types

ECMAScript includes two different types of values: the base data type and the reference data type.

The base data type refers to simple data segments, which refer to objects that have multiple values.

When we assign a variable to a variable, the parser first confirms whether the value is a base type value or a reference type value.

2. Common Basic data types:

Number, String, Boolean, NULL, and undefined. The base data type is accessed by value, because the actual value saved in the variable can be manipulated directly. Example:

  var a = 10;

  var b = A;

  B =;

  Console.log (a); 10 Value

Above, B gets a worth copy, although the values of two variables are equal, but two variables hold two different base data type values.

  b Just a copy of a copy is saved. So, the change of B has no effect on a.

  Demonstrates the process of assigning this basic data type:

3. Reference type data:

That is, the object type objects, such as: Object, Array, Function, data, and so on.

  The reference data type of JavaScript is the object that is stored in heap memory.

  Unlike other languages, you can not directly access the location in the heap memory space and manipulate the heap memory space. Only the reference address of the object in the stack memory can be manipulated.

  Therefore, the reference type data is actually stored in the stack memory as the object's reference address in the heap memory. This reference address allows you to quickly find objects in the stored heap memory.

  var obj1 = new Object ();

  var obj2 = obj1;

  Obj2.name = "I have a name";

  Console.log (Obj1.name); I have a name.

Describes the two reference data types pointing to the same heap memory object. Obj1 assigned to Onj2, actually this heap memory object in the stack memory of the reference address copied a copy to the OBJ2,

But in fact they point to the same heap of memory objects together. The actual change is the heap memory object.

  Let's demonstrate this reference data type assignment process:

4. Summarize the difference

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, it is not allowed to directly access objects stored in the heap memory, so when accessing an object, the first is the object in the heap memory address, and then follow this address to get the value of this object, this 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 a variable that holds the original value is copied to another variable, a copy of the original value is assigned to the new variable. Since these two variables are completely independent, they only 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, which means that the two variables point to the same object in the heap memory, and any changes that they make will be reflected in 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 the ECMAScript are is passed by value.Of 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. Article from link address: https://www.cnblogs.com/cxying93/p/6106469.html

Differences between basic data types and reference data types in JavaScript

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.