Table of Contents
Basic packaging type
Code status
Home Web Front-end JS Tutorial Analysis of basic packaging types in JavaScript

Analysis of basic packaging types in JavaScript

Oct 25, 2017 am 09:41 AM
javascript js type

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages ​​and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts Dec 18, 2023 pm 03:39 PM

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

How to create a video matrix account? What types of matrix accounts do it have? How to create a video matrix account? What types of matrix accounts do it have? Mar 21, 2024 pm 04:57 PM

With the popularity of short video platforms, video matrix account marketing has become an emerging marketing method. By creating and managing multiple accounts on different platforms, businesses and individuals can achieve goals such as brand promotion, fan growth, and product sales. This article will discuss how to effectively use video matrix accounts and introduce different types of video matrix accounts. 1. How to create a video matrix account? To make a good video matrix account, you need to follow the following steps: First, you must clarify what the goal of your video matrix account is, whether it is for brand communication, fan growth or product sales. Having clear goals helps develop strategies accordingly. 2. Choose a platform: Choose an appropriate short video platform based on your target audience. The current mainstream short video platforms include Douyin, Kuaishou, Huoshan Video, etc.

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

The relationship between js and vue The relationship between js and vue Mar 11, 2024 pm 05:21 PM

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.

What is the type of return value of Golang function? What is the type of return value of Golang function? Apr 13, 2024 pm 05:42 PM

Go functions can return multiple values ​​of different types. The return value type is specified in the function signature and returned through the return statement. For example, a function can return an integer and a string: funcgetDetails()(int,string). In practice, a function that calculates the area of ​​a circle can return the area and an optional error: funccircleArea(radiusfloat64)(float64,error). Note: If the function signature does not specify a type, a null value is returned; it is recommended to use a return statement with an explicit type declaration to improve readability.

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

The AI ​​era of JS is here! The AI ​​era of JS is here! Apr 08, 2024 am 09:10 AM

Introduction to JS-Torch JS-Torch is a deep learning JavaScript library whose syntax is very similar to PyTorch. It contains a fully functional tensor object (can be used with tracked gradients), deep learning layers and functions, and an automatic differentiation engine. JS-Torch is suitable for deep learning research in JavaScript and provides many convenient tools and functions to accelerate deep learning development. Image PyTorch is an open source deep learning framework developed and maintained by Meta's research team. It provides a rich set of tools and libraries for building and training neural network models. PyTorch is designed to be simple, flexible and easy to use, and its dynamic computation graph features make

See all articles