Easy to understand JS basic packaging objects

Source: Internet
Author: User

Today to discuss the basic JS in the packaging object (also known as the basic packaging type), just learned here when, oneself is also confused, do not understand this basic packaging object is what ghost, later found a lot of information, finally see its true colors. First of all, we now review the JS data type, JS data type is divided into two gate, basic type and reference type .

base type : Undefined,null,boolean,number,string

reference type : Object,array,date,regexp and so on, the object is plainly ...

As we all know, reference types have methods and properties, but the basic types are wood, but you must have seen code like this.

var // string Base type var s2 = str.charat (0);
alert (S2); H

There is no doubt that the above string is a basic type, but it can summon a charat () method, what is the reason?

Mainly because in the basic type, there are three more special existence is: String number Boolean, these three basic types have their own corresponding wrapper object. And waiting to be summoned at any time. Packaging objects, in fact, is the object, there are corresponding properties and methods. As to how this process happened, in fact, in the background secretly happened.

Take a look at chestnuts.

//we usually write the process of the program:varstr = ' Hello ';//string Base typevarS2 = Str.charat (0);//in the execution of this sentence, the following actions are automatically performed in the background:(var str = new String (' Hello '); //1 Locate the corresponding wrapper object type, and then create an object with the same value as the base type by wrapping the objectvar s2 = str.chaat (0); //2 The object can then invoke the method under the wrapper object and return the knot to S2.str = null;  //after 3, the temporarily created object is destroyed, str =null; ) alert (S2);//halert (str);//Hello Note that this is a momentary action we did not actually change the value of the string itself. is to do the following action. This is why each string has a method that does not change the string itself. 

From this we can see that the difference between a reference type and a basic wrapper object is: lifetime

A reference type creates an object that has been in memory for the duration of the execution, while the basic wrapper object only exists for an instant.

So we can't add a method to the base type directly:

Give me a chestnut.

varstr = ' Hello '; Str.number= 10;//Let's say we want to add a property number to the string, the following steps are in the background{var str = new String (' Hello ');// 1 Find the corresponding wrapper object type and then create an object with the same value as the base type by wrapping the object Str.number = ten;// 2 through this pair Like calling the method under the wrapper object but the results are not saved by anything str =null; 3 This object is destroyed again  . }alert (str.number);//undefined when executing to this sentence, because the primitive type has no attributes, the background repeats the above steps again{var str = new String (' Hello ');// 1 Find the basic wrapper object, and then open a new memory, create a value for the Hello object str.number = undefined// 2 because The wrapper object has no number this property, so it will be added again, because there is no value, so the value is undecided; and then pops the result str =null; 3 This object is destroyed again }

So how can we add methods or properties to the base type?

The answer is to add a prototype underneath the basic wrapper object, with each object having a prototype.

Take a look at chestnuts.

//add a method to a string to write to a prototype of the corresponding wrapper objectvarstr = ' Hello '; String.prototype.last=fuction () {return  This. CharAt ( This. length);}; Str.last (); //5 Implementation to this sentence, the backstage will still secretly do these things{var str = new String (' Hello ');//find the basic wrapper object, new one object with the same string value,str.last ();  //This object finds the method under the wrapper object and callsstr =null; //This object was destroyed .}

Look at the annotations and believe you can see that the methods and properties created under the Basic wrapper object prototype can be saved.

About the basic packaging objects on the chatter here, I hope we all have a harvest, study together, I am Mu Ching, a free and easy front-end sister, the next article is not scattered.

Easy to understand JS basic packaging objects

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.