JavaScript type conversion deep learning
JavaScript is a weakly typed language. It feels convenient and fast when you first come into contact with it (you don’t need to declare variable types!). After you have been exposed to it for a long time, you will find that the troubles it brings are sometimes unexpected.
Hahaha, what? It’s such an exaggeration. Someone may have seen such a piece of code
1 |
|
This takes up a lot of space, 3167 characters. Paste it into the console of the browser, and orange will pop up directly. Is there any one on call?
For If you don’t know the source of the principle, I’ll give you an address: http://www.jsfuck.com/
JSFuck is extremely perverted, because its concept is Write any JavaScript with 6 Characters: []()!+
Maybe some people say: This is just funny. Who actually writes code like this? You are right. When a piece of code becomes obscure and difficult to understand, even the confusing characters (heavenly book) above, it can achieve anything The function becomes unpredictable, which means that the security of JS code is not guaranteed
Of course, this article will not study how these meaningless character principles are implemented, because the Github document has already been described very comprehensively, if you are interested You can study it: https://github.com/aemkei/jsfuck
Let’s talk about how the underlying methods we see used every day are parsed. We know that there are two types of conversions, one is implicit conversion, and the other is It is a forced type conversion
Implicit conversion
When encountering the following situations, JavaScript will automatically convert the data type:
Different types of data perform mutual operations
Calculate Boolean values for non-Boolean type data
Use unary operators (i.e. "+" and "-") for non-numeric type data
Implicit conversion to Boolean
Most of them will be used when making if judgments. Here you only need to remember six conversions to false , all others are true
null
undefined
NaN
''
-0
+0
Implicit conversion to String
Automatic conversion of strings mainly occurs during addition operations. When one value is a string and the other value is a non-string, the latter is converted to a string.
1 2 3 4 5 6 7 8 |
|
Implicit conversion to Number
Except for the addition operator, which may convert the operator to a string, other operators will automatically convert the operators on both sides into numbers
1 2 3 4 5 6 7 8 9 10 11 12 |
|
The basic performance of implicit conversion is here , it is emphasized that these conversions are accompanied by forced conversion, using Boolean, Number and String. Let’s focus on the principle of forced conversion
Forced conversion
After seeing the above example, you may already have some questions, such as the above How come this '1' + {} outputs 1[object Object]? As emphasized above, you would guess that you first execute String({}) to get "[object Object]", and then concatenate the strings. Yes We can always get the implementation principle behind the conversion. In fact, the real principle is more complicated than this. See below
Forced conversion to Boolean
I skip it here because it is the same as implicit conversion. Remember that [] and {} are all converted to true
Forced conversion to String
The conversion result of the basic type is the same as the implicit conversion. Here we talk about the conversion of objects to deepen the analysis of the above cited example
Converting an object to a string is divided into three steps
First call the toString method. If the toString method If the toString method returns a value of a composite type, then call the valueOf method. If the valueOf method returns a value of the original type, Then use the String method on the value and no longer perform the following steps
If the valueOf method returns a composite type value, an error will be reported
Let’s break down this example again
1 2 |
|
The above code is equivalent to the following
1 2 |
|
If the toString method and The valueOf method returns values that are not of the original type, so the String method reports an error
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
We can easily see that the toString method and the valueOf method can be rewritten, and it is much simpler to test the order in which they are run successively
1 2 3 4 5 6 7 8 |
|
The result indicates the toString method Executed before the valueOf method
Forced conversion to Number
The basic type conversion is as follows
1 2 3 4 5 6 7 8 9 10 11 |
|
Object conversion is also more complicated. The only difference from String is that the valueOf method comes first and the toString method comes after. For other details, see the example above. .
isNaN() is not unfamiliar. The internal conversion process of isNaN({}) //true is the same
Summary
There are many other conversion principles. Seeing this, we still cannot explain the code conversion at the beginning of the article. Process, mastering these is more to ensure normal writing of code to avoid errors. If you are very curious, you can study the more special transformation principles, and there are many more.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

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

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
