To facilitate operation of the base type value, ECMAScript also provides three special reference types: Boolean, number, and string. These types are similar to other reference types and also have special behaviors corresponding to their respective basic wrapper types. In fact, whenever a primitive type value is read, the background creates an object of the corresponding basic wrapper type, allowing us to invoke some methods to manipulate the data.
var S1 = "some text"; var s2 = s1.substring (2);
The variable in this example S1 contains a string, and the string is, of course, the base type value. The next line calls the substring () method of S1 and saves the returned result in S2. We know that primitive type values are not objects, so logically they should not have methods (but they do have methods). In fact, in order for us to achieve this intuitive operation, the background has been automatically completed a series of processing. When the second line of code accesses S1, the access procedure is in a read mode, that is, the value of the string is read from memory. When accessing strings in read mode, the following processes are automatically completed in the background:
(1) Create an instance of type string.
(2) invokes the specified method on the instance.
(3) Destroy this instance.
You can use the following code to indicate:
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. Furthermore, the above three steps also apply to Boolean and numeric values that correspond to the Boolean and number types.
The main difference between a reference type and a basic wrapper type is the life cycle 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. Objects that are automatically created by the basic wrapper type exist only in the execution period (instantaneous) of this line of code and are immediately destroyed. This means that we cannot add properties and methods to the property at run time.
var S1 = "some text"; S1.color = "Red"; alert (S1.color); Undefined
Of course, you can display objects that call Boolean, number, and string to create a basic wrapper type, but it is not recommended to do so. Calling typeof on an instance of the base wrapper type returns "Object", and all objects of the basic wrapper type are converted to Boolean true:
var obj = new Object ("some text"), alert (obj instanceof String)//true
It is important to note that using new to invoke the constructor of the basic wrapper type is not the same as a transformation function that calls the same name directly.
var = "+"; var number = number (value);//Transformation function alert (typeof number)//number var obj = new number (VAR); Constructor alert (typeof obj)//object
Basic JavaScript Packaging type