Basic Package Type
The basic wrapper type is a special reference type . Whenever a primitive type value is read, the background creates an object of the corresponding basic wrapper type , which can be called by the property, method, and subsequent operation.
ECMAScript also provides three basic packaging types: number, String, Boolean.
Code Status
We often write code like this, the basic type of variable can be called as an object property, method, but this is not a real object is just a variable, then this is why?
1 var str = "Hello World"; 2 Console.log (str.length); 113 Console.log (Str.touppercase ());//HELLO World
In fact, in order to manipulate the value of the base data type, the value of the base data type in JavaScript creates a corresponding basic wrapper type Object in the background, with the exception of null and undefined all basic types have corresponding wrapper types.
As the code above, the following processing will be done automatically in the background:
Execution to second row:
- Creates an instance of the String type;
- Invokes the specified property on the instance;
- Destroy this instance;
Execution to third row:
- Creates an instance of the String type;
- Invokes the specified method on the instance;
- Destroy this instance;
The above three steps can be imagined as executing the following ECMAScript code:
1//Execute to second row 2 var str = new string ("Hello World"); 3 str.length;4 str = NULL;5 6//executes to third row 7 var str = new string ("Hello wo Rld "); 8 str.touppercase (); 9 str = null;
In this way, the string in the code becomes the same as the object, and the same applies to the basic data type corresponding to the Boolean and number types.
Is it clear that this basic package type is actually the type of the basic data type
Type mapping relationships
String |
String |
Number |
Number |
Boolean type (True, false) |
Boolean |
Attention:
Rather than providing a method for the wrapper type, the JavaScript engine creates an instance (object) of the corresponding basic wrapper type behind the scenes.
Each time a base type value is read, the background creates a corresponding basic wrapper type object such as:
1 var str = new String ("Hello World"); 2 str.length;3 str = null;
The difference between a reference type object and a basic wrapper type Object
The main difference is that the two objects have different life cycles.
With the code block above, we know that the basic wrapper type automatically creates an object that exists at the time the object is called, is destroyed when the call is finished, and an instance of the reference type that is created with the new keyword, and the object exists in the current scope.
Basic Wrapper Type Object
1 var s= ' some '; 2 s.age= ' Joel '; 3 Console.log (s.age);//undefined
As the above code adds the Age property to the string s, but when the code executes to the third line the Name property has been destroyed, and when the code executes the third line, a string instance is created, but this instance does not have an age attribute so the output undefined;
Show instantiation Wrapper type
1 var s=new String (' some '); 2 s.age= ' Joel '; 3 Console.log (s.age);//joel
Summarize
1. Each basic data type is mapped to a basic wrapper type with the same name (except null, undefined).
2. When reading the base type value, the corresponding basic wrapper type object is created, which facilitates the data operation.
3, the operation of basic packaging class objects once executed, will be destroyed immediately.
Basic JavaScript Packaging type