Get the App
SLTechnology News&Howtos  ›  Development  › 

Why a=abc is different from a=newStringabc

Shulou Source: shulou.com Published: 2022-06-03 13:55:22 10月24日 Update

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.

Tags: Type wrapper object content instance boxing original that is data procedure maximum obvious function God character string Boolean number method more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple macOS Microsoft Shulou Tech Info Huawei