最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Strange behaviour of JavaScript in Chrome Developer Tool - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question edited Jul 11, 2015 at 16:07 Alex Saskevich asked Jul 11, 2015 at 16:00 Alex SaskevichAlex Saskevich 3511 gold badge3 silver badges13 bronze badges 2
  • I do like how you called it a feature and not a bug – Dan Commented Jul 11, 2015 at 16:03
  • 1 I wrote a little article about it: medium./@asaskevich/… – Alex Saskevich Commented Jul 15, 2015 at 11:10
Add a ment  | 

2 Answers 2

Reset to default 7

This is the way chrome evaluates your input:

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

So once your input is }{ it bees

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

Next input }-+{ bees

undefined -+ {} // NaN

And so on.

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

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

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

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

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

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

发布评论

评论列表(0)

  1. 暂无评论