Chrome Developer Tool中JavaScript的奇怪行为

时间:2015-07-11 16:00:40

标签: javascript google-chrome google-chrome-devtools v8

最近,在Developer Tool中使用JavaScript,我发现了一个奇怪的功能。 Chrome接受开头括号与操作符(加号,减号)之间的任何代码和带闭括号的操作符并执行它,如下所示: enter image description here

我在其他浏览器中找不到此行为,仅在Chrome中。

也许它是一个功能,但它为什么以及如何工作,是否会成为JavaScript引擎的问题?

2 个答案:

答案 0 :(得分:6)

这是chrome评估输入的方式:

with (typeof __commandLineAPI !== 'undefined' ? __commandLineAPI : { __proto__: null }) {
 // your code here...
}

一旦您的输入为}{ 它变成了

with (typeof __commandLineAPI !== 'undefined' ? __commandLineAPI : { __proto__: null }) {}{} // indefined

下一个输入}-+{

undefined -+ {} // NaN

等等。

答案 1 :(得分:5)

这是因为Chrome以下列结构包装您在控制台中输入的代码:

with (typeof __commandLineAPI !== 'undefined' ? __commandLineAPI : { __proto__: null }) {
  // Your code
}

因此,当您输入} 10 {之类的内容时,代码评估为:

with (typeof __commandLineAPI !== 'undefined' ? __commandLineAPI : { __proto__: null }) {
  } 10 {
}

是空with块,数字和空结构块。

__commandLineAPI是包含Chrome Command Line API的内部对象。

相关问题