Basic Packing Type--ecmascript

Source: Internet
Author: User
Tags wrapper
Brief Introduction

(1) To facilitate operation of basic type values, ECMAScript also provides 3 special reference types: Boolean, number, and String

(2) These types are similar to other reference types but also have special behaviors corresponding to their respective basic types

(3) Whenever a basic type value is read, the background creates an object of the corresponding base wrapper type, allowing us to invoke methods to manipulate these data principle Instances

var S1 = "some text"; 
Analysis

1, the basic type value is not an object, so logically they should not have methods, in fact, in order to enable us to achieve this intuitive operation, the background has automatically completed a series of processing

2. When the second line of code accesses S1, the access procedure is in a read mode, which is to read the value of the string from memory. When you access a string in read mode, the background automatically completes the following processing

(1) Create an instance of String type

(2) Invoking the specified method on the instance

(3) Destroying this instance

The three steps above can be thought of as executing the following ECMAScript code:

var S1 = new String ("some text"); 
var s2 = s1.substring (2); 
Instance two
var S1 = "some text"; 
S1.color = "Red"; 
alert (S1.color);  
Analysis

difference between
The main difference between a reference type and a basic wrapper type is the lifetime of the object

(1) An instance of a reference type created using the new operator that is kept in memory until the execution stream leaves the current scope

(2) An object of the base wrapper type that is automatically created, only exists at the execution moment of a line of code and is immediately destroyed, so you cannot add properties and method instances to the base type value at run time three

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

The Object constructor, like the factory method, returns instance instance four of the corresponding base wrapper type based on the type of the passed-in value

var value = "a"; 
var number = number (value);  Transition function 
alert (typeof number);        ' Number ' 

var obj = new number (value);//constructor 
alert (typeof obj);           
Analysis

Invoking the constructor of the base wrapper type using new is not the same as calling a transformation function with the same name directly. In this example, the value of the base type is saved in the variable number 25, and the instance of number is saved in the variable obj.

1, the basic packing type other reference types are similar, but at the same time with their respective basic types corresponding to the special behavior

2, whenever reading a basic type value, the background will create a corresponding basic wrapper type object, so that we can call some methods to manipulate the data

3. The main difference between a reference type and a basic wrapper type is the lifetime of the object

(1) An instance of a reference type created using the new operator that is kept in memory until the execution stream leaves the current scope

(2) An object of the base wrapper type that is automatically created, only exists at the execution moment of a line of code and is immediately destroyed

4. The Object constructor, like a factory method, returns an instance of the corresponding base wrapper type based on the type of the passed-in value

5. Invoking the constructor of the base wrapper type using new is not the same as a transition function that calls the same name directly

6, you can explicitly invoke Boolean, number, and String to create objects of the base wrapper type. However, you should do this again when absolutely necessary, because it is easy to tell whether you are dealing with a basic type or a value reference of a reference type

"JavaScript Advanced Programming (3rd edition)"

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.