简体   繁体   English

Chrome开发人员工具中JavaScript的异常行为

[英]Strange behaviour of JavaScript in Chrome Developer Tool

Recently, working with JavaScript in Developer Tool, I found strange feature. 最近,在开发人员工具中使用JavaScript时,我发现了一个奇怪的功能。 Chrome accepts any code between opening bracket with operator (plus, minus sign) and operator with closing brackets and executes it, like this: Chrome接受带有运算符(加号,减号)的右括号和带有括号的运算符之间的任何代码,并执行该代码,如下所示: 在此处输入图片说明

I didn't find this behaviour in another browsers, just in Chrome. 我没有在其他浏览器中找到这种行为,只是在Chrome中。

Maybe it's a feature, but why and how it works, can it be problem with JavaScript engine? 也许这是一个功能,但是为什么以及它如何工作,JavaScript引擎会出现问题吗?

This is the way chrome evaluates your input: 这是chrome评估您的输入的方式:

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

So once your input is }{ it becomes 因此,一旦您输入了}{它就会变成

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

Next input }-+{ becomes 下一个输入}-+{变为

undefined -+ {} // NaN

And so on. 等等。

This happens because Chrome wraps the code you enter in the console in the following construction: 发生这种情况是因为Chrome以以下结构包装了您在控制台中输入的代码:

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

So, when you enter something like } 10 { , the code evaluates to: 因此,当您输入类似} 10 {的代码时,代码的计算结果为:

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

which is empty with block, a number, and empty structural block. 它是空的, with块,数字和空结构块。

__commandLineAPI is the internal object that contains Chrome Command Line API . __commandLineAPI是包含Chrome命令行API的内部对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM