Chrome开发工具中JavaScript的奇怪行为
最近,在开发工具中使用JavaScript时,我发现了一个奇怪的特性。Chrome接受带运算符(加号、减号)的开始括号和带结束括号的运算符之间的任何代码,并执行该代码,如下所示: 我没有在其他浏览器中发现这种行为,只是在Chrome中Chrome开发工具中JavaScript的奇怪行为,javascript,google-chrome,google-chrome-devtools,v8,Javascript,Google Chrome,Google Chrome Devtools,V8,最近,在开发工具中使用JavaScript时,我发现了一个奇怪的特性。Chrome接受带运算符(加号、减号)的开始括号和带结束括号的运算符之间的任何代码,并执行该代码,如下所示: 我没有在其他浏览器中发现这种行为,只是在Chrome中 也许这是一项功能,但为什么以及如何工作,这可能是JavaScript引擎的问题吗?之所以会出现这种情况,是因为Chrome将您在控制台中输入的代码包装为以下结构: with (typeof __commandLineAPI !== 'undefined' ? _
也许这是一项功能,但为什么以及如何工作,这可能是JavaScript引擎的问题吗?之所以会出现这种情况,是因为Chrome将您在控制台中输入的代码包装为以下结构:
with (typeof __commandLineAPI !== 'undefined' ? __commandLineAPI : { __proto__: null }) {
// Your code
}
因此,当您输入类似于}10{
的内容时,代码的计算结果为:
with (typeof __commandLineAPI !== 'undefined' ? __commandLineAPI : { __proto__: null }) {
} 10 {
}
它是空的,带有
块、数字和空结构块
\uu commandLineAPI
是包含的内部对象。这是chrome评估输入的方式:
with (typeof __commandLineAPI !== 'undefined' ? __commandLineAPI : { __proto__: null }) {
// your code here...
}
所以一旦你的输入是}{
它变成
with (typeof __commandLineAPI !== 'undefined' ? __commandLineAPI : { __proto__: null }) {}{} // indefined
下一个输入}-+{
变成
等等。我很喜欢你称它为功能而不是Bug的方式。我写了一篇关于它的小文章:注意:该对象被称为
\uu commandLineAPI
,而不是commandLineAPI
。同样值得注意的是,该对象代表Chrome。@gxoptg当然,似乎是redactor解析我的代码的结果;)
undefined -+ {} // NaN