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

Example Analysis of javascript data Type

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

Share

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

This article will explain in detail the sample analysis of javascript data types. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

1. Undefined and null

Undefined is a variable, not a keyword, so it can be reassigned. To avoid ambiguity, it is generally recommended to use void 0 to obtain undefined

Null is a keyword, so you can safely use null.

Undefined is somewhat different from null in that null means "defined but empty". Therefore, in actual programming, we generally do not assign variables to undefined, which ensures that all variables with values of undefined are in a natural state that has never been assigned.

Function test () {var undefined = 123; console.log (undefined)} test () = > 123

2. + 0 and-0

There are + 0 and-0 in JavaScript. There is no difference between them in addition operations. To distinguish them, you need to use 1Compx.

3Take 0 = > Infinity3/-0 = = >-Infinity

3. 0.1 + 0.2 = 0.3?

In JS, 0. 1 to 0. 2 is not equal to 0. 3, because 0. 1 is expressed in binary in a computer, resulting in an infinite number of looping digits. Similar to the decimal system, 1/3 is an infinite recurring decimal.

So when you perform mathematical operations on these cyclic decimals and convert the binary data to decimal, it is also a wireless cyclic decimal. After truncating the end, the value is 0.30000000000000004.

The correct comparison method is to use the minimum precision value provided by JS to check whether the absolute value of the difference between the left and right sides of the equation is less than the minimum precision.

Math.abs (0. 1 + 0. 2-0. 3) 1*6+5parseInt (0x15) 21 = > 16 / 1 / 5

ParseFloat parses the original string as a decimal directly, and it does not introduce any other base.

6. Box conversion

Each basic type Number, String, Boolean, and Symbol has a corresponding class in the object. The so-called boxing conversion is to convert the basic type to the corresponding object.

Symbol is special. You can't get the corresponding object directly with new Symbol, but you can get it with Object (Symbol ('hello')).

Var symbolObject = Object (Symbol ("a")); console.log (typeof symbolObject); / / object console.log (symbolObject instanceof Symbol); / / true console.log (symbolObject.constructor = = Symbol); / / true

7. Unpacking conversion

When an object is converted to number or string, unboxing is performed by default, that is, the object's built-in toString or valueOf method is called to convert the object to the basic type.

An obj object is defined here, and its valueOf and toString methods are overridden for testing. You can see that when Number (obj), valueOf takes precedence over String, but an error occurs when neither of these two methods returns the basic type of data.

When the valueOf method returns data of the basic type, the obj is successfully converted to a number, and the toSring method is not executed.

The toString method is called only when valueOf returns a non-primitive type. The toString method is called first only when the unboxing transformation from Object to String takes precedence.

Note: ES6 allows objects to override their original behavior by explicitly specifying toPrimitive Symbol.

Var o = {valueOf: () = > {console.log ("valueOf"); return {}}, toString: () = > {console.log ("toString"); return {}} o [Symbol.toPrimitive] = () = > {console.log ("toPrimitive") This is the end of the article return "hello"} console.log (o + ") / / toPrimitive / / hello on" sample Analysis of javascript data types ". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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