JavaScript reference types and basic types detailed _javascript tips

Source: Internet
Author: User

There are two types of data in JavaScript: base type and reference type.

The basic type is simply a simple data segment.

A reference type is an object that consists of multiple values.

When we do an assignment, the parser first parses whether the data is a value type or a reference type.

Two ways of accessing:

Basic type values: accessed by value, the value they actually save is the operation;

Reference type value: accessed by reference, when the query, we need to read from the stack of memory address, and then follow the trace to find the value stored in the heap memory;

Here are the two types of JavaScript data.

I. Basic data types:

There are six basic types of data in javascript: string, number, Boolean, symbol (ES6 new), null, undefined.
These five basic data types can directly manipulate the actual values saved in the variable.

The code example is as follows:

var a=10;
var b=a;
b=20;
Console.log (a);

The above code is a simple assignment operation, below do a brief introduction.

(1). The first number is the basic data type.
(2). var b=a, which actually copies a copy of the data of a, and assigns the value to variable B.
(3). A and B are completely independent.
(4). b=20, modifying the value of variable B does not affect the value of variable A.

This is illustrated below:

Stack memory


Two. Reference type data:

In JavaScript, reference type data is stored in heap memory, but the location in the heap memory space and the operating heap memory space cannot be directly accessed.
Only the reference address in the stack memory of the object is manipulated. So the reference type of data, stored in the stack memory is actually the object in the heap memory reference address. This reference address allows you to quickly find objects that are saved in heap memory.

Code instance:

var obj1=new Object ();
var obj2=obj1;
Obj2.name= "cloud-dwelling community";
Console.log (Obj1.name);

Here's a little bit of code for the code above.

(1). Var obj1=new object (), which creates an object, is a reference type data, and the variable Obj1 stores the object's address in heap memory.
(2). var obj2=obj1, this assignment is actually to copy the object in the heap memory address to the variable obj2, that is, two variables are stored to point to the actual object of memory address, pointing to the same object.
(3). Obj2.name= "cloud-dwelling community" to add an attribute to the object.
(4). Console.log (obj1.name), output "cloud-dwelling community, because two variables point to the same object."

This is illustrated below:


String a special basic data type

In many languages, string is represented as an object, but this tradition is not used in ECMAScript, and string is treated as a basic data type, but it is a relatively special basic type.

It looks as if string should be a reference type, but it's not actually because it's not an object. So it looks like it should be the basic data type and it should be the way the pass value is passed.

Look at the following example:

var stra = "This is a string";
var strb = stra;
Stra = "This is another string";

As we can see in the example above, it is as if stra a copy of the STRB by value. When the stra changed, the STRB did not change, as if we had been able to conclude that string was a basic data type.

However, because a string can be arbitrary in length and passed by value, one copy byte display efficiency remains low, and it seems that a string can also be used as a reference type.

Look at the following example:

var a = "MyObject";
A.name = "MyName";

Display string cannot be treated as an object. In fact, the string in JavaScript cannot be changed, and JavaScript does not provide any method or syntax to change the string.

var a = "MyObject";
A = a.substring (3,5)

Remember to do this without changing the string string "MyObject", only a refers to another string "BJ", and "MyObject" is recycled.

So it can be said that a string does not actually conform to the above two data type classifications. It is a special type with two properties between them.

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.