Home > Article > Web Front-end > 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
[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]][([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]((![]+[])[+!+[]]+(![]+[])[!+[]+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]+(!![]+[])[+[]]+(![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[!+[]+!+[]+[+[]]]+([]+[])[(![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(!![]+[])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]()[+!+[]+[!+[]+!+[]]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]+(![]+[])[+!+[]]+([][[]]+[])[+!+[]]+(+![]+[![]]+([]+[])[([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]])[!+[]+!+[]+[+[]]]+(!![]+[])[!+[]+!+[]+!+[]]+([]+[])[(![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(!![]+[])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]()[+!+[]+[!+[]+!+[]]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[!+[]+!+[]+[+[]]])()
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 // '12' '1' + true // "1true" '1' + false // "1false" '1' + {} // "1[object Object]" '1' + [] // "1" '1' + function (){} // "1function (){}" '1' + undefined // "1undefined" '1' + null // "1null"
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
'5' - '2' // 3 '5' * '2' // 10 true - 1 // 0 false - 1 // -1 '1' - 1 // 0 '5' * [] // 0 false / '5' // 0 'abc' - 1 // NaN +'abc' // NaN -'abc' // NaN +true // 1 -false // 0
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
String({}) // "[object Object]"
The above code is equivalent to the following
String({}.toString()) // "[object Object]"
If the toString method and The valueOf method returns values that are not of the original type, so the String method reports an error
var obj = { valueOf: function () { console.log("valueOf"); return {}; }, toString: function () { console.log("toString"); return {}; } }; String(obj) // TypeError: Cannot convert object to primitive value
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
String({toString:function(){return 3;}}) // "3" String({valueOf:function (){return 2;}}) // "[object Object]" String({valueOf:function (){return 2;},toString:function(){return 3;}}) // "3"
The result indicates the toString method Executed before the valueOf method
Forced conversion to Number
The basic type conversion is as follows
Number("123") // 123 Number("123abc") // NaN Number("") // 0 Number(false) // 0 Number(undefined) // NaN Number(null) // 0
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.