最近,在Developer Tool中使用JavaScript,我发现了一个奇怪的特性。铬接受与运算符(加,减号)和运营商开括号之间的任何代码右括号并执行它,就像这样: Chrome开发工具中JavaScript的奇怪行为
我没有找到在其他浏览器中这种行为,只是在Chrome中。
也许这是一个功能,但它为什么以及如何工作,它可以成为JavaScript引擎的问题吗?
最近,在Developer Tool中使用JavaScript,我发现了一个奇怪的特性。铬接受与运算符(加,减号)和运营商开括号之间的任何代码右括号并执行它,就像这样: Chrome开发工具中JavaScript的奇怪行为
我没有找到在其他浏览器中这种行为,只是在Chrome中。
也许这是一个功能,但它为什么以及如何工作,它可以成为JavaScript引擎的问题吗?
这是Chrome的方式评估你的输入:
with (typeof __commandLineAPI !== 'undefined' ? __commandLineAPI : { __proto__: null }) {
// your code here...
}
所以一旦你输入}{
成为
with (typeof __commandLineAPI !== 'undefined' ? __commandLineAPI : { __proto__: null }) {}{} // indefined
下一页输入}-+{
成为
undefined -+ {} // NaN
等等上。
注意:该对象被称为'__commandLineAPI',而不是'commandLineAPI'。另外值得注意的是,这个对象表示Chrome [命令行API](https://developer.chrome.com/devtools/docs/commandline-api)。 –
@gxoptg肯定,似乎是redactor如何解析我的代码的结果;) –
这是因为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的内部对象。
我喜欢你如何称它为功能而不是bug –
我写了一篇关于它的文章:https://medium.com/@asaskevich/how-does-chrome-executes-scripts-inside-developer- tool-59a5ea8f7de2 –