Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why a=abc is different from a=newStringabc

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Today, I would like to talk to you about why a=abc is different from a=newStringabc. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.

obvious

A = "abc" typeof a / / stringb=new String ("abc") typeof b / / objecta==b / / truea===b / / false

But why? Read a lot of books, asked several great gods, in fact, I am still confused. Make a record here for reference.

In js, distinguish between source data type and wrapper type. Numbers, strings, Boolean, null, undefined belong to the source data type, while Number, String, and Boolean belong to the wrapper type, and new Number creates a derived object of the wrapper type. So the two are not the same.

The process after directly assigning a value to a basic type is as follows:

1. Create an instance of String type

2. Call the specified method in the instance

3. Destroy the instance

For example:

Var a = "123" a.toFixed===Number.prototype.toFixed

Here is another saying: boxing, unpacking

Boxing is to use this value class to construct a corresponding wrapper object

Var axiom 10, b = "javascript", centering truth; var o_a=new Number (a); var o_b=new String (b); var o_c=new Boolean (c)

The greatest function of boxing is to treat values as objects.

Unpacking, which converts the wrapper object to a value type

Var await 10; var o_a=new Number (a); var b=o_a.valueOf (); / / this is the process of unpacking. After reading the above, do you have any further understanding of why a=abc is different from a=newStringabc? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report