Home >Web Front-end >JS Tutorial >Analysis of basic packaging types in JavaScript
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;In this way, the strings in the above code become the same as objects. The same applies to the basic data types corresponding to Boolean and Number types respectively;
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;The difference between reference type objects and basic packaging type objectsThe main difference is that the life cycles of these two objects are different. Through the above code block, we know that the object automatically created by the basic packaging type exists when the object is called, and is destroyed after the call is completed. However, for instances of reference types created using the new keyword, the object is currently scope exists.
s='some' s.age='Joel' console.log(s.age);The above code adds the age attribute to the string s, but when the code is executed on the third line, the name attribute has been destroyed. When the code is executed on the third line, , another String instance is created, but this instance does not have an age attribute, so the output is undefined;
s= String('some' s.age='Joel' console.log(s.age);Summary1. Each basic data type Maps to the basic packaging type of the same name (except null, undefined).
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!