Home > Web Front-end > JS Tutorial > Analysis of basic packaging types in JavaScript

Analysis of basic packaging types in JavaScript

黄舟
Release: 2017-10-25 09:41:08
Original
1432 people have browsed it

Basic 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
Copy after login

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. Create an instance of the String type;

  2. Call the specified attribute on the instance;

  3. Destroy the instance;

Execute to the first Three lines:

  1. Create an instance of the String type;

  2. Call the specified method on the instance;

  3. Destroy this instance;

You can imagine the above three steps as executing the following ECMAScript code:


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;
Copy after login

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

stringStringnumberNumberBoolean type (true, false)Boolean
type mapping relationship

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;
Copy after login

The difference between reference type objects and basic packaging type objects

The 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);
Copy after login

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);
Copy after login
Summary

1. 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template