In order to facilitate the operation of basic type values, ECMAScript also provides 3 special reference types: Boolean, Number and String. These types are similar to the other reference types introduced in this chapter, but also have special behavior corresponding to their respective base types. In fact, whenever a basic type value is read, an object of the corresponding basic wrapper type will be created in the background, allowing us to call some methods to manipulate this data. Example below:
The variable s1 in this example contains a string. The string is of course a basic type value, and the next line of code 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 (although, as we wish, they do). In fact, in order for us to achieve this intuitive operation, a series of processes have been automatically completed in the background. When the second line of code accesses s1, the access process is in a read mode, that is, the value of this string is read from memory. When accessing a string in read mode, the following processing will be automatically completed in the background.
1. Create an instance of String type;
2. Call the specified method on the instance;
3. Destroy this instance.
You can think of the above three steps as executing the following ECMAScript code.
After this processing, the basic string value becomes the same as the object, and the above three steps are also applicable to Boolean and numeric values corresponding to the Boolean and Number types respectively.
The main difference between reference types and basic packaging types is the lifetime of the object. Instances of reference types created using the new operator remain in memory until the execution flow leaves the current scope. The automatically created basic packaging type object only exists at the moment when a line of code is executed, and then is destroyed immediately. This means that we can no longer add properties and methods to primitive type values at runtime. As an example below:
Here, the second line of code attempts to add a color attribute to the string s1. However, when the third line of code accesses s1 here, its color property is missing. The cause of the problem 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 a color attribute.
Of course, you can explicitly call Boolean, Number and String to create objects of basic packaging types. However, you should only do this when absolutely necessary, because it is easy to lose sight of whether you are dealing with a base wrapper type or referencing a value of a base wrapper type. Calling typeof on an instance of a basic wrapper type returns "object", and all objects of the basic wrapper type are converted to the Boolean value true.
The Object constructor will also, like a factory method, return an instance of the corresponding basic wrapper type based on the type of value passed in. For example:
Passing a string to the Object constructor will create an instance of String; passing in a numeric parameter will get an instance of Number, and passing in a Boolean parameter will get an instance of Boolean.
It should be noted that using new to call the constructor of the basic packaging type is different from directly calling the transformation function of the same name. For example:
In this example, the variable number stores the basic type value 25, and the variable obj stores an instance of Number.
Although we do not recommend explicitly creating objects of basic wrapper types, their ability to manipulate basic type values is still quite important. Each basic wrapper type provides convenient methods for manipulating the corresponding value.
That’s all the content of this article, I hope you all like it.