带有字符串的左移位数

时间:2016-01-20 03:27:47

标签: javascript

B.array = dic[@"products"];

[cartItems removeAllObjects];
NSLog(@"%@", B.array); ///< B.array won't be empty

我在数字和字符串之间应用左移,结果为10000,如何在JavaScript中解释?

10000<<"5daz"

1 个答案:

答案 0 :(得分:0)

Javascript通常会尝试解析其运算符的参数类型,以便它们“有意义”。在这种情况下,它会向String投射Uint32,以便它可以执行左移。将String投射到Uint32时,会先将其投射到NumberIf the number is NaN, then the UInt32 is 0 by the specification.

10000<<"5daz"10000<<0相同,10000

更多阅读,here in the specification

在其他操作中,NaN的处理方式不同,这可能会让您对<<运算符感到惊讶。例如,addition将参数视为Number s,而不是Uint32。出于这个原因10000 + "5daz" = NaN10000 << "5daz" = 10000