Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么(+;+;';';)==0?_Javascript_Syntax - Fatal编程技术网

Javascript 为什么(+;+;';';)==0?

Javascript 为什么(+;+;';';)==0?,javascript,syntax,Javascript,Syntax,有人能解释一下这种奇怪的javascript行为吗 + // Rightfully gets a syntax error, because there are no operands + + // Same as above + + '' // Interpreted as 0 typeof(+ + '') // Interpreted as "number" + + '' === 0 // Interpreted as true 换句话说,(++'')被计算为零。1) 这在语法上是

有人能解释一下这种奇怪的javascript行为吗

+
// Rightfully gets a syntax error, because there are no operands

+ +
// Same as above

+ + ''
// Interpreted as 0

typeof(+ + '')
// Interpreted as "number"

+ + '' === 0
// Interpreted as true
换句话说,(++'')被计算为零。1) 这在语法上是怎么允许的?2) 为什么计算为零?

将右侧转换为数字


所以你有0← 0← 空字符串。

+
是一元加号运算符,用于将某物转换为数字
+“”
相当于
Number(“”
,即
0
运算符具有指定的行为。对操作数进行运算。JavaScript是松散类型的。当运算符对给定运算符的有效操作数进行操作时,如果需要,它将使用类型强制执行其操作。因此,剩下的唯一问题是,在对特定类型的操作数进行操作时,特定运算符的行为如何。这在中以及互联网上的许多其他资源中都有很好的记录。有点让你喜欢强类型。学习半打像这样令人困惑的比较是我们为避免键入每一件小事而付出的代价。但这不应该是字符串类型吗,因为我附加了一个空字符串?typeof(0+“”)=“string”@carlbenson:没有附加/连接
+(+)
如果你用它来代替
+(+)
,那将是一个不同的故事,它将是
(+(+)
。请参见此处的JS运算符的优先级和关联性: