What is a wrapper object

Source: Internet
Author: User

JS provides 11 types of references: Object, Array, Date, Function, Error, REGEXP, Math, number, String, Boolean, globle. This includes three basic wrapper types, number, String, and Boolean, which are special reference types that are similar to other reference types and have special behaviors corresponding to their original types.

First, what is the type of packaging?

Because this string, numeric value, Boolean three primitive types of values, under certain conditions will be automatically converted to an object, so is called the original type of "wrapper type."

For example: Var a1= "some text";

var a2=a1.substring (0,2);

Console.log (A2);//"So"

In the above example, the variable A1 is a string, the character creation is necessarily the value of the primitive type, but it calls the substring () method on the second line and saves the result back in A2, and we all know that since A1 is the value of the base type and not the object, logically it is not a method. In fact, in the implementation of this operation, JS internal has implicitly helped us to create a wrapper object, the actual situation above should be this:

var a1=new String ("some text");

var a2=a1.substring (0,2);

A1=null;

Console.log (A2);//"So"

But there is a difference between the two ways:

Difference one: The browser implicitly creates the wrapper object and the wrapper object that you explicitly created is not strictly equal.

var a1= "Test";

var a2=new String ("test");

Console.log (A1==A2);//true

Console.log (A1===A2);//false

Difference 2: Implicitly created wrapper objects are destroyed after they are used. For example:

var s1= "some text";

var s1.color= "Red";

Console.log (S1.color);//undefined

The above code, in the second line of code, tries to add a color property to a character S1, but when the third row is accessed again, the Color property is gone. The reason is that the string object created by the second line of code was destroyed when the third line of code was executed, and the third line of code created its own string object, but without the color property, the final value returned is undefined.

In practice, we can explicitly invoke string, number, and Boolean to create an object of the basic wrapper type, but you should do so when absolutely necessary, because it's easy to tell whether you're dealing with a basic type or a reference type. Calling typeof on an instance of the base wrapper type returns an object.

Ii. the difference between the type of packaging and the type of reference

The primary difference between a reference type and a wrapper type is 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 of the basic package-to-type of the auto-dress, however, exists only during the execution of one line of code and is immediately destroyed, which also means that we cannot add properties and methods to the base type at run time.

What is a wrapper object

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.