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

What is the implicit transformation of js?

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how the implicit transformation of js is, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Preface

A question was raised in a previous article [[js's criticized feature]].

I thought the question was simple at first. Think that as long as you remember some features. Therefore, the exhaustive method is directly used to summarize the law.

But when the result of console.log (Number ([])) is 0, and the result of console.log (Number ([1,2])) is NaN. What a mess, there must be something wrong in it. Although you can memorize it by heart, as an aspiring programmer, you still have to figure out what it's all about.

Console.log ({}-{}) / / NaNconsole.log ([]-[]) / 0console.log ([]-[1,2]) / / NaNconsole.log ([] = =! []) / / trueconsole.log ({} = = {}) / / false

To understand the results printed above is to understand the value of Number ([]), the value of Number ([1,2]), the value of Number ({}), and the value returned by Boolean ([]). Speak slowly next.

1. Wrapper Boolean ()

There are only two results of Boolean, true and false.

The result of Boolean is the type of false, which we usually call falsey and imaginary value in Chinese. These values are mentioned in [[js's criticized feature]], that is,

0, null, undefined, false,'', NaN

Some articles count-0 and + 0 as two.

These are the original values to the original values. The rest are true.

If the reference value is changed to the original value, it is true. There are also the following reference types that you can't remember for a while.

Boolean (/ d /) Boolean (new Error ()) console.log (Boolean (Symbol () extension

There is also an easy to confuse typeof return result, in a node environment:

Console.log (typeof Date ()) console.log (typeof new Date ()) console.log (Date ()) / / Thu Jan 13 202l2 22:29:36 GMT+0800 (China Standard time) console.log (new Date ()) / / 2022-01-13T14:29:36.660ZNumber () reference type conversion Number

The error-prone point appears on Number (). In particular, the reference type is converted to the original type. Knowing this, the initial example can be understood.

Only when we know the rules of Number (reference type) can we judge the result of converting a reference type into an original type, otherwise it is impossible to judge hunger, and we can't go far by guessing.

Let's assume that there is such an object as follows

Const obj = {toString () {return 2}, valueOf () {return 1}}

We Number () wrap it.

Console.log (Number (obj))

The printed result is 1. Oh, here comes the fun.

Consot obj = {toString () {return 2}, valueOf () {return {}} console.log (Number (obj)) / / 2

Let's print out 2 when the value returned by valueOf is an object, otherwise print out the original value of String directly and convert it to number type.

Generally speaking, valueof represents a value, which is meaningless and does not need to be dealt with. What the value is, for example, [1, 2, 3] .valueof () printing directly is [1, 2, 3].

We can also solve the problem of [let a = = 1 & & a = = 2 & a = = 3] by this method.

So it's easy to get a rule: when the return value of valueOf is a reference type, take the value returned by toString. In terms of expansion, it is:

If valueOf returns the original value, it will be returned after the Number wrapper

If the object returned by valueOf, look for it in the toString () method

If toString () returns the original value, it is returned after the Number wrapper

If toString () returns an object and is rewritten by itself. Then report the error directly.

If it is not congested, then call the Obejct.prototye.toString method

Obviously, the problem of [prototype chain] is also involved here, so the problem of implicit transformation is not as simple as imagined.

The object we created basically did not say that we would create these two methods. So it's obviously a method that inherits from Object. In other words, what we are looking at is the value returned by Object.prototype.toString.call ().

Console.log (Object.prototype.tostring.call ('123')); console.log (Object.prototype.toString.call (123)); console.log (object.prototype.tostring.call (true)); console.log (Object.prototype.tostring.call (undefined)); console.log (Object.prototype.tostring.call (null)); console.log (Object.prototype.tostring.call (function) {})); console.log (Object.prototype.toString.call ([1J 2J 3])) Console.log (Object.prototype.tostring.call (ff))

The printed results are as follows:

[object string] [object Number] [object Boolean] [object Undefined] [object Null] [object Arrayl] [object objectl] primitive type to Numberconsole.log (Number (undefined)) / / NaNconsole.log (Number (null)) / / 0console.log (Number (true)) / / 1console.log (Number (false)) / / 0console.log (Number (NaN)) / / NaNconsole.1og (Number (Infinity)) / / Infinityconsole.log (Number (') / / 0console.log (Number ('') / / 0console.log (Number ('123')) / / 123

There's nothing to say above, just memorize it. It is important to note that the value of Number, in addition to the numerical meaning we usually use, there are also NaN, Infinity.

There are also these confusion points that need to be noted: console.log ((123) .toString ()) / / 123console.log (undefined.toString ()) / / error reporting console.log (nul.toString ()) / / error reporting

Undefined and null do not have wrapper classes, and they are basic types, so there are no other garbled methods. So report an error.

Expansion

Argument and document

Console.log (Object.toString.call (argument)) / / [object Argument] console.log (Object.toString.call (document)) / / [object HTMLDocument]

HTMLDocument is the type of object provided to us by the browser.

From this, we can see that the wide application of Object.prototype.toString method and the identification of many types are much more powerful than the crude return value of typeof. Of course, each has its own use of the scene.

Handwritten typeof

Typeof comes with jscore and is not a grammatical sugar. I was confused when I saw this interview question at first. Do you want to write the engine code of typeof by hand? Explain that the typeof null that has existed since the first version of js is object?

But there are really some companies to take this test, a little wise, smart. So I read other people's interpretation of this on the Internet. Using the result returned by Object.prototype.toString.call (), the string is cut, and then the latter word is returned.

That's it? Take off your pants and fart.

Number conversion to non-binary

Number (0xfff) / / 4095Number / / 56

Number can directly identify different digits and convert them into decimal systems.

The relationship between parseInt and Number

Number ('123abc') / / NaNNumber (' ad123') / / NaNNumber ('123') / / 123

And [[parseInt]] solves this problem very well, it can be said to be a good supplement to Number ().

ParseInt ('123abc') / / 123parseInt (' 123asd1') / / 123parseInt ('ad123') / / NaNparseInt (' 123') / / 123String () Object.prototype.toString

For the use of String (), we still use the example of Number ().

Const obj = {toString () {return 2}, valueOf () {return 1}}

When I trigger String (obj), it's the exact opposite of Number ().

Console.log (String (obj)) / / 2

The toString () method is accessed directly.

Const obj = {toString () {return {}}, valueOf () {return 1}} console.log (String (obj)) / / 1

But if toString () returns a reference type, look above the valueOf () method. It can be said that it is completely opposite to that of Number (), but it is also reasonable.

It is a good way to understand the internal running rules by overriding toString () and valueOf ().

If not rewritten, Object.prototype.toString.call (object), the return value can be found in the Number () section.

Console.log (String ({})) / / [object Object] Array.prototype.toString

There is nothing to say in this memory, just tear down the outside [].

Console.log (String ([1])) / / '1'console.log (String ([1,2])) / /' 1, 2'console.log (Array.prototype.toString.call ([1])) / / '1'console.log (Array.prototype.toString.call ([1,2])) / /' 1,2' 2, implicit conversion trigger rule

It is said earlier that the law of display transformation. Here are the rules that can trigger implicit conversions.

The rules for and operators are and [precedence of operators], which are not mentioned here, but can be checked for yourself.

Implicit Transformation of Boolean

When judgment occurs, there is an implicit transformation.

If,switch,while,for (;;), & &, | |,!,!,?: implicit conversion of ternary number

As long as you have primary school knowledge, you know the operator, which is used for the calculation between numbers. It is basically the same in JavaScript.

+-*% = = ~ & | ~ ^

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