简体   繁体   English

在JavaScript中使用+ +时的奇怪行为

[英]Weird behavior when using + + in JavaScript

Everybody knows the basic concatenation of two strings in JavaScript: 每个人都知道JavaScript中两个字符串的基本连接:

> "Hello " + "World!"
'Hello World!'

But what happens if we use + + instead of + ? 但是如果我们使用+ +代替+会发生什么? I just encountered the following weird behavior: 我刚刚遇到了以下奇怪的行为:

> "Hello " + + "World!"
'Hello NaN'
> "Hello " + + ""
'Hello 0'

From the examples above, I can see that the second string is converted into number. 从上面的例子中,我可以看到第二个字符串被转换为数字。 So, passing an object having valueOf property as function, the value returned by that function will be converted. 因此,将具有valueOf属性的对象作为函数传递,将转换该函数返回的值。

> "Hello " + + ({valueOf: function () {return 1; }})
'Hello 1'

As expected, it shows "Hello 1" . 正如预期的那样,它显示"Hello 1"


Why is the second string converted in Number ? 为什么第二个字符串转换为Number Why not throwing a syntax error or so? 为什么不抛出语法错误呢?

The second + is the unary plus operator whose purpose is to convert its operand to a number. 第二个+一元加运算符,其目的是将其操作数转换为数字。

The unary plus operator precedes its operand and evaluates to its operand but attempts to converts it into a number, if it isn't already. 一元加运算符在其操作数之前,并计算其操作数,但尝试将其转换为数字(如果尚未)。 Although unary negation (-) also can convert non-numbers, unary plus is the fastest and preferred way of converting something into a number, because it does not perform any other operations on the number. 虽然一元否定( - )也可以转换非数字,但是一元加号是将某些东西转换为数字的最快和首选方式,因为它不会对数字执行任何其他操作。 It can convert string representations of integers and floats, as well as the non-string values true, false, and null. 它可以转换整数和浮点数的字符串表示形式,以及非字符串值true,false和null。 Integers in both decimal and hexadecimal ("0x"-prefixed) formats are supported. 支持十进制和十六进制(“0x” - 前缀)格式的整数。 Negative numbers are supported (though not for hex). 支持负数(但不支持十六进制)。 If it cannot parse a particular value, it will evaluate to NaN. 如果它无法解析特定值,它将评估为NaN。

"Hello " + + "World!"

can only be parsed as 只能解析为

"Hello " + (+ "World!")

which is 是的

"Hello " + NaN

hence your result. 因此你的结果。

This unary operator is very useful in JavaScript and is one of the most common ways to convert from something which may be a number or a string to a number. 这个一元运算符在JavaScript中非常有用,并且是从可能是数字或字符串转换为数字的最常用方法之一。 It also has the advantage over parseFloat that it's not ridiculously tolerant ( parseFloat("7up") would return 7 ) which makes it an easy way to see if a string is the representation of a number (just test s==+s ). 它还具有优于parseFloat的优点,它不是可笑的容忍( parseFloat("7up")将返回7 )这使得它很容易查看字符串是否是数字的表示(只是测试s==+s )。

Great question. 好问题。 This is because JavaScript has a unary + operator (similar to the unary - as in, ex, -1 , which is parsed to - (1) ): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Unary_plus_(.2B) 这是因为JavaScript有一个一元+运算符(类似于一元-如in,ex, -1 ,它被解析为- (1) ): https//developer.mozilla.org/en-US/docs/Web /JavaScript/Reference/Operators/Arithmetic_Operators#Unary_plus_(.2B)

Thus "foo" + + "bar" is parsed to "foo" + (+ "bar") (or if you prefer a lisp-style parse tree: (+ "foo" (+ "bar") ). 因此, "foo" + + "bar"被解析为"foo" + (+ "bar") (或者如果你更喜欢lisp风格的解析树: (+ "foo" (+ "bar") )。

The unary + converts its operand to a number, or NaN if the operand isn't a sensible number: 一元+将其操作数转换为数字,如果操作数不是合理的数字,则转换为NaN

> +"42"
42
> +"foo"
NaN

Finally, adding a number (including NaN ) to a string results in a string concatenation. 最后,向字符串添加数字(包括NaN )会导致字符串连接。

Thus "foo" + +"bar" can be expanded: 因此可以扩展"foo" + +"bar"

"foo " + +"bar"
"foo " + NaN
"foo NaN"

Here + is a unary plus operator Which tries to convert the operator in number & works as follows 这里+是一个一元加运算符,它试图转换运算符的数量并按如下方式工作

+"" //return 0
+"Some" //returns NaN

So here 所以在这里

"Hello " + + "World!" 

will be 将会

"Hello " + (+ "World!")
"Hello " + NaN
"Hello Nan"

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

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