Strange behaviour of JavaScript in Chrome Developer Tool

只谈情不闲聊 提交于 2019-12-30 08:33:02

问题


Recently, working with JavaScript in Developer Tool, I found strange feature. Chrome accepts any code between opening bracket with operator (plus, minus sign) and operator with closing brackets and executes it, like this:

I didn't find this behaviour in another browsers, just in Chrome.

Maybe it's a feature, but why and how it works, can it be problem with JavaScript engine?


回答1:


This is the way chrome evaluates your input:

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.




回答2:


This happens because Chrome wraps the code you enter in the console in the following construction:

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

So, when you enter something like } 10 {, the code evaluates to:

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

which is empty with block, a number, and empty structural block.

__commandLineAPI is the internal object that contains Chrome Command Line API.



来源:https://stackoverflow.com/questions/31359023/strange-behaviour-of-javascript-in-chrome-developer-tool

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!