Deep understanding of JavaScript Why string can have methods _javascript tips

Source: Internet
Author: User
Tags wrapper

Introduction

As we all know, JavaScript data types are divided into two broad categories, the base type (or the original type), and the reference type.

The values of the base type are simple data segments that are stored in stack memory and are accessed by value. There are five basic types in JS: Undefined, Null, Boolean, number, and string.

The value of a reference type is an object that is saved in the heap memory, and its value is accessed by reference. Reference types are mainly object, Array, Function, RegExp, Date.

Objects have properties and methods, so it's not surprising that we see the code below.

var favs=[' eggs ', ' lotus '];
Favs.push (' okra ');
Console.log (favs);//["eggs", "lotus", "okra"]
console.log (favs.length);//3

An array is a reference type, so it can naturally have attributes (length) and method (push), which is just like eating ice cream in summer. But, look at the following code, think about it, this is this, legal?

var realmessage= "Said I love you but I lied";
var mymessage=realmessage.substring (5,15);
Console.log (Mymessage); "I Love You"

There is a broken-hearted woman paper wayward to a string to break up the wayward execution of the "substring" method, and then happy to watch the clip version of the sleep passed. But but, not that string is a basic type, why does it have methods?? There is no the Royal Sky Big Master!

In fact, all of this is because there is a "basic packaging type" Dongdong. This basic type of packaging is particularly straightforward, is the real "thing of the brush to go, deep work and name"!

Basic Packing Type

In addition to reference types such as Object, array, and so on, JavaScript also provides us with three special reference types: String, Number, and Boolean to facilitate our operation of the corresponding base type.

Continuing to look at the example of the clip string above, has it ever been noted that, despite the use of the substring method, the value of the realmessage itself will not change, calling this method simply returns a new string.

This is the role of the basic packing type. Originally you do not have method, but you want to use method of time, you although adjust, the corresponding basic packing type has this method on line. For example, the substring method above, string this basic type is impossible to have this method, but string this type of wrapper has Ah, it will chi the method to execute the result returned. In execution to:

Realmessage.substring (5,15) This line of code, a lot of things happened.

First, it reads the value of the realmessage from memory. When in this mode of reading, the backstage began to work. JS elevation describes these actions that are done in the background:

1. Create an instance of string type;

2. Invokes the specified method on the instance;

3. Destruction of this instance

The above example can be illustrated with this code:

var _realmessage=new String ("Said I love you but I Lied");
var mymessage=_realmessage.substring (5,15);
_realmessgae=null; Method is destroyed after the call

So, then we know, instead of the base type string, which executes its own method, it creates a corresponding base wrapper type string in the background, which, based on the value of the base type, instantiates an instance that invokes the specified method, and finally destroys itself, feeling that there is wood in the sky.

Note the last step of the basic wrapper type "destroy" feature, which determines that we cannot add custom properties and methods for base type values.

var me= "sunjing";
me.age=18;
Console.log (me.age);//undefined

I added the age attribute to the "Me" string, and the value was set to a beautiful 18-year-old, and then the egg, once again, had no trace of the property. This is because:

When you perform an assignment to the second line of code properties, the background creates an instance of the base wrapper type, and the age attribute does hang up to the instance, but immediately following this instance is destroyed. A new instance of the base wrapper type was recreated when the third row was executed, and naturally there was no age attribute.

Show use basic wrapper type

In addition to the string in read mode, the background will help us create the basic wrapper type instance, we can also display the creation.

var str=new String ("Hello");
var str2=str.touppercase ();
Console.log (STR2);//"HELLO:

This is different from the things that are saved in the background to help us create the time variable.

var str1=new String ("Hello");
var str2= "Hello";
typeof str1//"Object"
typeof str2//"string"

Summarize

Thanks to the basic packing type, it is more convenient for us to operate the three basic types of string, Boolean, and number. Each time the three basic type values are read, the background creates the corresponding wrapper type instance, which invokes the specified method and is destroyed when the call is finished. This short-lived lifecycle determines that we cannot add custom properties and methods to the base type.

The above in-depth understanding of JavaScript why string can have a method is a small series to share all the content of the people, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.