To facilitate manipulation of basic type values, ECMAScript also provides 3 special reference types: Boolean, number, and string. These types are similar to other reference types described in this chapter, but also have special behavior corresponding to their respective base types. In fact, whenever reading a basic type is worth it, the background creates an object of the corresponding base wrapper type, allowing us to call some methods to manipulate the data. The following example:
Copy Code code as follows:
var s1= "some text";
var s2=s1.substring (2);
The variable S1 in this example contains a string, which is of course the base type value, and the next line of code calls the S1 substring () method and saves the returned result in S2. We know that basic type values are not objects, so logically they should not have methods (although they do have a way, as we would like to say). In fact, in order for us to achieve this intuitive operation, the background has automatically completed a series of processing. 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 a string is accessed in read mode, the following processing is done automatically by the background.
1. Create an instance of string type;
2. Invokes the specified method on the instance;
3. Destroy this instance.
The three steps above can be thought of as executing the following ECMAScript code.
Copy Code code as follows:
var s1=new String ("some text");
var s2=s1.substring (2);
S1=null;
After this process, the basic string value becomes the same as the object, and the above three steps apply to Boolean and numeric values corresponding to the Boolean and number types respectively.
The main difference between a reference type and a base wrapper type is the lifetime of the object. An instance of a reference type created using the new operator is kept in memory until the execution stream leaves the current scope. 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. This means that we cannot add properties and methods to the base type values again when we run. As in the following example:
Copy Code code as follows:
var s1= "some text"
S1.color= "Red";
alert (s1.color);//undefined
Here, the second line of code attempts to add a color property to the string S1. However, when the third line of code accesses S1, its color property is missing. The cause of the problem is that the string object created in the second row was destroyed when it executed the third line of code. The third line of code creates its own string object, which does not have a color property.
Of course, the droplets that can be displayed call 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 wrapper type or a value that refers to the base wrapper type. Calling typeof on an instance of the base wrapper type returns "Object", and all objects of the base wrapper type are converted to the Boolean value true.
The object constructor, like a factory method, returns an instance of the corresponding base wrapper type based on the passed-in worthy type. For example:
Copy Code code as follows:
var obj=new Object ("some text");
Alert (obj instanceof String);//true
Passing the string to the object constructor creates an instance of string, and passing in the numeric argument gets an instance of number, and the passed-in Boolean argument gets the Boolean instance.
Note that using new to invoke the constructor of the base wrapper type is not the same as a transition function that invokes the same name directly. For example:
Copy Code code as follows:
var value= "25";
var number=number (value); Transformation function
Alert (typeof number); "Number"
var obj=new number (value); Constructors
Alert (typeof obj); "Object"
In this example, the variable number holds the value 25 of the base type, and the variable obj holds an instance of number.
Although we do not recommend explicitly creating objects of the basic wrapper type, their ability to manipulate basic type values is quite important. Each basic wrapper type provides a convenient way to manipulate the corresponding value.
The meaning is the whole content of this article, I hope you can enjoy it.