JavaScript data Base types and reference types

Source: Internet
Author: User

JavaScript basic Data type:

JS basic data types include: Undefined,null,number,boolean,string. The base data type is accessed by value, which means that we can manipulate the actual values that are saved in the variable.

  

1. The value of the base data type is immutable

No method can change the value of a primitive type, such as a string:

var name = "Change"; Name.substr ();//hangconsole.log (name);//changevar s = "Hello"; s.touppercase ()//hello; Console.log (s)//hello

Through these two examples, we will find that the value of the previously defined variable name is always unchanged, and a new string is returned after calling the substr () and toUpperCase () methods, which is not related to the previously defined variable name.

  

Perhaps someone will have the following questions, see the code:

var name = "Change"; name = "Change1"; Console.log (name)//change1

This looks like the name value "changed" in fact, var name = "Change", here the underlying type is a string, that is, "changes", where the "change" can not be changed, the name is just a pointer to "changes", Pointers can be changed, so you can name = "Change1″." At this point the name points to "Change1″, similarly," Change1″ here can not be changed. in other words, the change you think of here is just a "pointer change". The underlying type here refers to "change", not name, to distinguish it clearly.

2. Basic data types can not add properties and methods
var p = "Change";p. Age = 29;p.method = function () {console.log (name)};console.log (p.age)//undefinedconsole.log ( P.method)//undefined

With the code above, we know that we cannot add properties and methods to the base type, and again that the basic type is immutable.

3. Assignment of basic data type is simple assignment

  If a value of the base type is assigned to another variable from one variable, a new value is created on the variable object, and the value is copied to the location assigned to the new variable .

var a = 10;var B = A;a++;console.log (a)//11console.log (b)//10

In the above code, the value saved in a is 10. When the value of a is used to initialize B, the value 10 is also saved in B. But 10 of 10 and a in B are completely independent. The value in B is a copy of the value in knowledge a. So these two variables can participate in any operation without affecting each other.

4. Comparison of basic data types is a comparison of values
var Person1 = ' {} '; var person2 = ' {} '; Console.log (Person1 = = Person2); True

5. The basic data type is stored in the stack area

If there are several basic types of variables:

var name = "Jozo"; var city = "Guangzhou"; var = 22;

  

Then its storage structure is as follows:

The stack area includes the variable's identifier and the value of the variable.

JavaScript Reference type:

JS In addition to the above basic type is a reference type, can also be said to be an object, such as: Object,array,function,data.

  

1. The value of a reference type can be changed
var o = {x:1};o.x = 2;//changes the object by modifying the object property value O.y = 3; Change the object again, add an attribute to it var a = [1,2,3];a[0] = 0;//change an element of an array a[3] = 4;//Add an element to the array

2. Reference types can add properties and methods
var person = {};p erson.name = "Change";p Erson.say = function () {alert ("Hello");} Console.log (Person.name)//changeconsole.log (Person.say)//function () {alert ("Hello");}

3. Assignment of a reference type is an object reference

Let's look at the following code:

var a = {};var b= a;a.name = "Change"; Console.log (A.name)//change;console.log (b.name)//changeb.age = 29;console.log ( A.age)//29console.log (b.age)//29

When a value of a reference type is assigned to another variable from one variable, The value of the object stored in the variable is also copied into the space allocated for the new variable. The reference type is stored in the variable as the address of the object in the heap memory, so unlike the simple assignment of the base data type, the copy of the value is actually a pointer to an object stored in the heap memory. After the assignment, two variables The same object address is saved, and the two addresses point to the same object. Therefore, changing any of these variables will affect each other.

Their relationship is as follows:

Therefore, the assignment of the reference type is actually the assignment of the object to the stack address pointer, so two variables point to the same object, and any operation will affect each other.

4. Comparison of reference types is a reference comparison
var Person1 = {};var Person2 = {};console.log (Person1 = = Person2)//false

Why do two objects look the same, but not equal?

Because the comparison of reference types is a reference comparison, in other words, if the addresses of two objects that are stored in the stack are the same as the address of the heap memory, the P1 and P2 appear to be a "{}", but the address to the heap memory they hold in the stack is different. So two objects are not equal.

5. Reference types are stored in both the stack area and the heap area.

The storage of the type needs to be done together in the stack and heap areas of the memory, and the stack holds the variable identifiers and addresses that point to the heap memory .

If there are several objects:

var Person1 = {name: "Change1"};var person2 = {name: "Change2"};var Person3 = {name: "Change3"};

Basic packing type (wrapping object):

Let's look at the following code:

var S1 = "HelloWorld"; var s2 = s1.substr (4);

We said that the string is the basic data type, there should be no method, then why S1 here can call substr ()?

By flipping through the JS authoritative guide Chapter 3.6 and the Advanced Programming section 5.6, we learned that ECMAScript also provides three special reference type Boolean,string,number. We call these three special reference types the basic wrapper type, also called the wrapper object.

This means that when reading the three basic data types of String,boolean and number, the background creates a corresponding basic wrapper type object, which allows us to invoke some methods to manipulate the data.

So when the second line of code accesses S1, the following actions are done automatically in the background:

    1. Create an instance of the String type;//var S1 = new String ("HelloWorld");
    2. Invokes the specified method on the instance;//var S2 = s1.substr (4);
    3. Destroy this instance;//S1 = null;

Because there is a third step to this destruction action, you should be able to understand why the basic data types cannot add properties and methods, This is the primary difference between the basic package type and the reference type: The lifetime of the object. An instance of a reference type created with the new operator is kept in memory until the execution flow leaves the current scope. An object that is automatically created with a basic wrapper type exists only in the execution of a single line of code and is immediately destroyed.

JavaScript data Base types and reference types

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.