10000<<"5daz"我在数字和字符串之间应用左移,结果是10000,它在JavaScript中是如何解释的?
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Variables</h1>
<p>In this example, x, y, and z are variables</p>
<p id="demo"></p>
<script>
var x = 5;
var y = 6;
var z = x + y;
var bvalue=true;
document.getElementById("demo").innerHTML = 10000<<"5daz";
</script>
</body>
</html>发布于 2016-01-20 12:00:39
Javascript通常会尝试将参数类型解析为其运算符,以便它们“有意义”。在本例中,它将String转换为Uint32,以便可以执行左移。当将String转换为Uint32时,它首先将其转换为Number。If the number is NaN, then the UInt32 is 0 by the specification.
10000<<"5daz"与10000<<0相同,即10000
有关更多阅读,请访问here in the specification。
在其他操作中,NaN被区别对待,这可能是为什么<<操作符会让您感到惊讶的原因。例如,Number将参数视为加法,而不是Uint32。由于这个原因,10000 + "5daz" = NaN而10000 << "5daz" = 10000
https://stackoverflow.com/questions/34891052
复制相似问题