JavaScript value types and reference types

Source: Internet
Author: User

First, introduction

ECMAScript contains two different types of values: primitive type values and reference type values. A primitive type value refers to a simple data segment; a reference type value refers to an object that consists of multiple values. When we assign a variable to a variable, the first thing the parser does is to verify that the value is a base type value or a reference type value. (JavaScript Advanced Programming (3rd edition))

Second, basic type value and reference type value

(1) Basic type value

Includes undefined, Null, Boolean, number, and string. These 5 basic data types are accessed by value, manipulating the actual values that are saved in the variable.

Example:

var aa = 10;var bb =aa;console.log (AA); 10console.log (BB); 10bb=11;console.log (AA); 10console.log (BB); 11

The above example, BB Gets the value is a copy of the AA value, although the value of the two variables is equal, but two variables save a copy of the basic data type value, completely independent. Therefore, when the value of BB is changed, the value of AA is still 10;

Demonstrate:

(2) Reference type value

Includes function, Object, Array. Copy of a reference type variable: Copies the pointer stored in the stack, copies the pointer to the space in the stack where the new variable is allocated, and the pointer copy and the original pointer execute the same object stored in the heap.

Example:

The object is a heap that is stored in memory. In the above example, B is a copy of a, and A and B is actually stored in the stack address, pointing to the address of the array object, after the copy operation, A, b two variables will actually refer to the same object, so changing one of them will affect the other.

Demonstrate:

Third, type detection

typeof can detect Undefined, Null, Boolean, number, string, and object. But it is not possible to detect whether it is an array, then there is a detection method instanceof.

var s = ' Hello '; var n =1;var u;var b =true;var nu=null;var o =new object (); var a = new Array (); Console.log (typeof s); Stringconsole.log (typeof N); Numberconsole.log (typeof u); Undefineconsole.log (typeof B); Booleanconsole.log (typeof Nu); Objectconsole.log (typeof O); Objectconsole.log (typeof a); Objectconsole.log (o instanceof Object); True Console.log (a instanceof Array); True Console.log (o instanceof RegExp); False


  

Iv. issues

So how do you back up the data for the reference type? I can't find the answer to the question of changing the array into a basic type.

JavaScript value 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.