简体   繁体   English

为什么“+”运算符可以将字符串解析为数字?

[英]Why does the “+” operator work in parsing string to number?

From what I have poorly remembered before, javascript tries to convert the operands to a similar type before doing the operation.从我之前不太记得的情况来看,javascript 会在执行操作之前尝试将操作数转换为类似的类型。 However, when I tried to do the following in the console:但是,当我尝试在控制台中执行以下操作时:

+'11111'
*'11111'

They produce different results - addition parses, multiplication fails.它们产生不同的结果 - 加法解析,乘法失败。 This means what I remembered was wrong.这意味着我记得是错误的。 What is the exact reason that + works? + 起作用的确切原因是什么? I have also seen people use it in IIFEs我也看到人们在 IIFE 中使用它

+(function() {

})()

//the same as:

;(function() {

})()

+'11111' results in the number 11111 because unary + converts the expression on its right side to a number. +'11111'结果是数字11111因为一元 +将其右侧的表达式转换为数字。 But there's no such thing as "unary *", so *'11111' throws an error.但是没有“一元 *”这样的东西,所以*'11111'会抛出一个错误。 (What would *'11111' even mean?) *'11111'甚至是什么意思?)

As for the IIFEs:至于 IIFE:

;(function() {

})()

the ; ; ensures that the IIFE starts after the end of the previous statement.确保 IIFE上一条语句结束开始。 Were it not for the ;如果不是为了; , and the previous statement did not end with a ; , 并且前面的语句没有以;结尾; , the lack of Automatic Semicolon Insertion on the previous line could cause bugs: , 上一行缺少自动分号插入可能会导致错误:

 const arr = [] (function() { })()

But using + without a semicolon on the previous line is not a good idea, because then the return value of the IIFE can be added or concatenated to the previous line:但是在前一行使用不带分号的+并不是一个好主意,因为这样可以将 IIFE 的返回值添加或连接到前一行:

 const arr = [] +(function() { })() console.log(arr);

If you always put semicolons at the end of statements when appropriate, then there's no need to put anything before the ( of an IIFE:如果你总是在适当的时候把分号放在语句的末尾,那么就没有必要在 IIFE 的(之前放任何东西:

 const arr = []; (function() { console.log('iife'); })(); console.log(arr);

(If you aren't an expert, I'd recommend using semicolons, rather than relying on the slightly strange rules of ASI - they'll sometimes result in hard-to-understand bugs) (如果您不是专家,我建议您使用分号,而不是依赖于ASI有点奇怪的规则——它们有时会导致难以理解的错误)

There is a good explanation and examples here and here for JavaScript Unary Operators. 此处此处对 JavaScript 一元运算符有很好的解释和示例。

TDLR: The unary operation tries to convert anything to a number, and if it can't success it will return NaN TDLR:一元运算尝试将任何东西转换为数字,如果不能成功将返回 NaN

+3                                   // returns 3
+'-3'                                // returns -3
+'3.14'                              // returns 3.14
+'3'                                 // returns 3
+'0xFF'                              // returns 255
+true                                // returns 1
+'123e-5'                            // returns 0.00123
+false                               // returns 0
+null                                // returns 0
+'Infinity'                          // returns Infinity
+'infinity'                          // returns NaN
+function(val){  return val }        // returns NaN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM