![](/img/trans.png)
[英]javascript selection sort causing chrome browser console to hang or crash why?
[英]Why does the chrome browser console forcibly coerce a string to a number in javascript?
新手问题。 我正在学习“您不知道JS”,我在第11页上发现了此异常:
var a = "42";
var b = Number( a );
console.log( a ); // "42"
console.log( b ); // 42
现在,如果我在JS Bin中运行它,便会获得该行为。 但是在我正在学习的最新的Chrome浏览器控制台中,我得到了这种意外的行为:
var a = "42";
var b = Number(a);
console.log(a);
console.log(b);
42 // no quotes?
42
< undefined
尝试从数字到字符串手动强制转换,这也是意外的结果:
a = 42;
b = a.toString();
console.log(a);
console.log(b);
42
42 // no quotes?
< undefined
有人可以阐明这种行为吗? 我用谷歌搜索了一下,加油。
值中没有带引号的事实是由于字符串不包含任何引号作为其内容 。 因此,除非是这种情况,否则您不应期望产生报价。
为了消除任何疑问,您始终可以使用typeof
检查类型:
var a = "42"; var b = Number(a); var c = '"String with quotes as part of the content."'; var d = 'String with NO quotes as part of the content.'; console.log(a, typeof a); console.log(b, typeof b); console.log(c, typeof c); console.log(d, typeof d);
请记住,特定浏览器决定实现其console
行为的方式完全取决于他们。 由于console
不是JavaScript的一部分,因此没有任何标准。 因此,在这方面,发现一个客户与另一个客户之间的差异是完全正常的。 重要的是JavaScript运行时根据标准处理数据。
Chrome console.log(..)输出始终不带引号。 尝试看看:
console.log("asd")
并在控制台中尝试:
var a = "42";
a;
应该返回想要的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.