The basic packaging type is a special reference type. Whenever a basic type value is read, a corresponding basic packaging type object will be created in the background, so that properties and methods can be called for subsequent operations.
ECMAScript also provides three basic packaging types: Number, String, and Boolean.
We often write code like this. Basic type variables can be used as objects to call properties and methods, but this is not a real object, just a variable, so this Why?
1 var str = "hello world"; 2 console.log(str.length); // 11 3 console.log(str.toUpperCase());// HELLO WORLD
In fact, in order to facilitate the operation of basic data type values, the basic data type value in JavaScript will create a corresponding Basic packaging type object## in the background. #, except null and undefined, all basic types have corresponding packaging types.
For the above code, the background will automatically complete the following processing: When executing the second line:1 // 执行到第二行时 2 var str = new String("hello world"); 3 str.length; 4 str = null; 5 6 // 执行到第三行时 7 var str = new String("hello world"); 8 str.toUpperCase(); 9 str = null;
Now do you understand the original basic packaging type? In fact, it is the type corresponding to the basic data type
String | |
Number | |
Boolean |
Note:
It is not that the packaging type provides methods, but that the javascript engine creates instances (objects) of the corresponding basic packaging types in the background )
Whenever a basic type value is read, the background will create a corresponding basic packaging type object such as:
1 var str = new String("hello world"); 2 str.length; 3 str = null;
s='some' s.age='Joel' console.log(s.age);
s= String('some' s.age='Joel' console.log(s.age);
2. When reading a basic type value, a corresponding basic packaging type object will be created, thus facilitating data operations.
3. Once the object of the basic packaging class is completed, it will be destroyed immediately.
The above is the detailed content of Analysis of basic packaging types in JavaScript. For more information, please follow other related articles on the PHP Chinese website!