Basic packaging types in javascript and javascript Packaging

Source: Internet
Author: User

Basic packaging types in javascript and javascript Packaging

To facilitate the operation of basic type values, ECMAScript also provides three special reference types: Boolean, Number, and String. These types are similar to other reference types described in this chapter, but they also have special behaviors corresponding to their basic types. In fact, every time a basic type is read, an object of the corresponding BASIC packaging type will be created in the background, so that we can call some methods to operate on the data. Example:

Copy codeThe Code is as follows:
Var s1 = "some text ";
Var s2 = s1.substring (2 );

In this example, the variable s1 contains a string, which is of course a basic type value. The next line of code calls the substring () method of s1 and stores the returned results in s2. We know that basic type values are not objects, So logically they should not have methods (although they do have methods as we wish ). In fact, in order to achieve this intuitive operation, the background has automatically completed a series of processing. When the second line of code accesses s1, the access process is in a read mode, that is, reading the value of this string from the memory. When accessing strings in Read mode, the background will automatically complete the following processing.
1. Create an instance of the String type;
2. Call the specified method on the instance;
3. Destroy the instance.

The preceding three steps can be considered as executing the following ECMAScript code.

Copy codeThe Code is as follows:
Var s1 = new String ("some text ");
Var s2 = s1.substring (2 );
S1 = null;

After this processing, the basic string value becomes the same as the object, and the preceding three steps apply to the Boolean and Number values corresponding to the Boolean and Number types respectively.

The main difference between the reference type and the basic packaging type is the lifetime of the object. Instances of the reference type created using the new operator are stored in the memory until the execution stream leaves the current scope. The automatically created basic packaging type object only exists in the execution instant of a line of code and is immediately destroyed. This means that we cannot add attributes and methods for basic type values at runtime. For example:

Copy codeThe Code is as follows:
Var s1 = "some text"
S1.color = "red ";
Alert (s1.color); // undefined

Here, the second line of code tries to add a color attribute for string s1. However, when the third line of code accesses S1. The cause is that the String object created in the second line has been destroyed when the third line of code is executed. The third line of code creates its own String object, which does not have the color attribute.

Of course, you can call Boolean, Number, and String to create an object of the basic packaging type. However, this should be done if absolutely necessary, because it is easy to tell whether you are dealing with the basic packaging type or referencing the value of the basic packaging type. If typeof is called for an instance of the basic packaging type, "object" is returned, and all objects of the basic packaging type are converted to a Boolean value of true.

The Object constructor returns an instance of the corresponding BASIC packaging type based on the input value type, just like the factory method. For example:

Copy codeThe Code is as follows:
Var obj = new Object ("some text ");
Alert (obj instanceof String); // true

If you pass the String to the Object constructor, a String instance is created. If you pass in a value parameter, the Number instance is obtained. If you pass in a Boolean parameter, a Boolean instance is obtained.

Note that using new to call constructors of the basic packaging type is different from directly calling transformation functions of the same name. For example:

Copy codeThe Code is as follows:
Var value = "25 ";
Var number = Number (value); // Transformation Function
Alert (typeof number); // "number"
Var obj = new Number (value); // Constructor
Alert (typeof obj); // "object"

In this example, the value 25 of the basic type is saved in the variable number, and the instance of the Number is saved in the variable obj.

Although it is not recommended to explicitly create objects of the basic packaging type, their ability to operate on basic type values is still very important. Each basic packaging type provides a convenient way to operate the corresponding value.

This is all about this article. I hope you will like it.

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.