Analysis of basic packaging types in JavaScript
Oct 25, 2017 am 09:41 AMBasic packaging type
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.
Code status
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:- Create an instance of the String type;
- Call the specified attribute on the instance;
- Destroy the instance;
- Create an instance of the String type;
- Call the specified method on the instance;
- Destroy this instance;
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!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Recommended: Excellent JS open source face detection and recognition project

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts

Simple JavaScript Tutorial: How to Get HTTP Status Code

How to create a video matrix account? What types of matrix accounts do it have?

How to get HTTP status code in JavaScript the easy way

What is the type of return value of Golang function?
