Learn about the basic types and reference types of JavaScript _javascript tips

Source: Internet
Author: User
Tags wrapper

One, basic type and reference type

There are 5 basic data types:undefined,boolean,number,string,null

typeof null; "Object"
typeof undefined;//"Undefined"
typeof 1;//"number"
typeof false//"Boolean"
typeof "1"// "String"

(Confusingly, the result of a typeof operation on a null type is "Object", however, the ECMAScript standard describes it as a unique type.) )

To facilitate manipulation of basic type values, ECMAScript also provides three special reference types: Boolean, number, and string, and the standard library provides constructors to encapsulate Boolean values, numbers, and strings as objects. These types are similar to other reference types and also have special behaviors corresponding to their respective base wrapper types. In fact, whenever a base type value is read, the background creates an object of the corresponding base wrapper type, allowing us to invoke methods to manipulate the data.

var S1 = "some text";
var s2 = s1.substring (2);
var s3 = new String ("some text");

But unlike the original string, the string object is a real object.

typeof S1; "String"
typeof S3;//"Object"

The variable S1 in this example contains a string, and the string is of course the base type value. The next line calls the S1 substring () method and saves the returned results in S2. We know that basic type values are not objects, so logically they should not have a method (but they do have a method). In fact, in order for us to achieve this intuitive operation, the background has automatically completed a series of processing. When the second line of code accesses S1, the access procedure is in a read mode that reads the value of the string from memory. When you access a string in read mode, the following processing is done automatically in the background:

(1) Create an instance of type string.

(2) invokes the specified method on the instance.

(3) Destroy this instance.

You can use the following code to represent:

var S1 = new String ("some text");
var s2 = s1.substring (2);
S1 = null;

After this process, the basic string value becomes the same as the object. Furthermore, the above three steps also apply to Boolean and numeric values corresponding to the Boolean and number types.

Second, life cycle

The main difference between a reference type and a basic wrapper type is the life cycle of the object. An instance of a reference type created using the new operator is kept in memory until the execution stream leaves the current scope. An object of the base wrapper type that is automatically created exists only in the execution period of this line of code (instantaneously) and then destroys immediately. This means that we cannot add properties and methods to the property at run time.

var S1 = "some text";
S1.color = "Red";
alert (S1.color); Undefined

Of course, you can display objects that invoke Boolean, number, and string to create the base wrapper type, but do not recommend doing so. Calling typeof on an instance of the base wrapper type returns "Object", and all objects of the base wrapper type are converted to a Boolean value of true.

var obj = new Object ("some text");
Alert (obj instanceof String)//true

It is worth noting that invoking the constructor of the base wrapper type using new is not the same as calling a transition function with the same name directly.

var value = "a";
var number = number (value);/transition function
alert (typeof number)//number

var obj = new number (var);//Constructor
alert ( typeof obj)//object

Three, the basic type characteristic

1. The value of the base type is not to be made:

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

var name = ' Jozo ';
Name.touppercase (); Output ' Jozo '
console.log (name);/output ' Jozo '

You will find that the original name has not changed, but instead call the toUpperCase () method and return a new string.
Let's look at another one:

var person = ' Jozo ';
Person.age =;
Person.method = function () {//...};

Console.log (Person.age); Undefined
console.log (person.method);//undefined

From the code above, we can not add properties and methods to the basic type, again, the basic type can not become;

2. Comparison of basic types is a comparison of values:

They are equal only when their values are equal.
But you might be like this:

var a = 1;
var B = true;
Console.log (A = = B);//true

Aren't they equal? In fact, this is the knowledge of type conversions and = = operators, which means that some type conversions occur when you compare two variables of different types with = =. A comparison like the one above will convert true to number 1 and the number 1, and the result is true. This is when the two values of the comparison of different types of the = = operator will be type conversion, but when the same type of two values, even = = = = = =.

var a = ' Jozo ';
var b = ' Jozo ';
Console.log (A = = = b);//true

3. The basic type of variable is stored in the stack area (stack area refers to memory stack memory)

If there are several basic types of variables:

var name = ' Jozo ';
var city = ' Guangzhou ';
var age = 22;

Then its storage structure is shown as follows:

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

Iv. Characteristics of reference types

Reference types are more fun and interesting.

JavaScript in addition to the basic type (number,string,boolean,null,undefined) is a reference type, it can be said to be the object. Objects are collections of properties and methods. That is, reference types can have properties and methods, and properties can contain basic types and reference types. Take a look at some of the attributes of reference types:

1). The value of the reference type is variable

We can add properties and methods to a reference type, or delete its properties and methods, such as:

var person = {};//Create a Control object--Reference type
person.name = ' Jozo ';
Person.age =;
Person.sayname = function () {console.log (person.name);} 
Person.sayname ();//' Jozo ' delete

person.name;//delete the Person object's Name property
person.sayname ();//undefined

The code above shows that reference types can have properties and methods and can be dynamically changed.

2. The value of a reference type is an object that is kept in both stack memory and heap memory

JavaScript is different from other languages, it does not allow direct access to the location in memory, that is, can not directly manipulate the object's memory space, then what do we operate? In fact, a reference to the action object, so the value of the reference type is accessed by reference.

To be precise, the storage of a reference type requires the memory stack area and the heap area (which is the heap memory in memory) to be completed together, the stack area memory holds the variable identifier and the pointer to the object in the heap memory, or the address of the object in the heap memory.
If there are several objects:

var Person1 = {name: ' Jozo '};
var Person2 = {name: ' Xiaom '};
var Person3 = {name: ' Xiaoq '};

These three objects are saved in memory in the following figure:

3). Comparison of reference types is a reference comparison

var Person1 = ' {} ';
var Person2 = ' {} ';
Console.log (Person1 = = Person2); True

When the comparison of the basic types is mentioned above, when the two comparison values are of the same type, it is the equivalent of = = =, so the output is true. Look again:

var person1 = {};
var person2 = {};
Console.log (Person1 = = Person2); False

You may have seen the flaw, compared to the above two strings, compared to the following two objects, why the long identical objects are not equal?

Remember, reference types are accessed by reference, in other words, if the addresses in the heap memory of two objects are the same, it is obvious that Person1 and Person2 address in heap memory are different:

So these two are completely different objects, so return false;

V. Simple Assignment

When you assign a basic type to another variable from one variable, a new value is created on the variable, and then the value is copied to the location assigned to the new variable:

var a = ten;
var b = A;

A + +;
Console.log (a); One
Console.log (b);//10

At this point, the value saved in a is 10, when you use a to initialize B, the value saved in B is also 10, but 10 in B is completely independent from a, which is just a copy of the value in a, which can then participate in any operation without affecting each other.

That is, the base type after the assignment operation, two variables are not affected by each other.

VI. Object reference

When you assign a value of a reference type to another variable from one variable, the same value of the object stored in the variable is also copied into the space allocated for the new variable. Referring to the reference type mentioned earlier, the address of the object in the heap memory is stored in the variable, so unlike a simple assignment, the copy of this value is actually a pointer to an object stored in the heap memory. Then after the assignment, the two variables all hold the same object address, the two variables point to the same object. Therefore, changing any one of these variables will affect each other:

var a = {}; A holds an instance of an empty object
var b = A;//A and B all point to this empty object

a.name = ' Jozo ';
Console.log (A.name); ' Jozo '
console.log (b.name);//' Jozo '

b.age =;
Console.log (b.age)/
Console.log (a.age);/

Console.log (A = = B);/True

Their relationship to the following figure:

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

The above is the entire content of this article, I hope to help you learn.

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.