Reputation: 351
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?
Upvotes: 8
Views: 896
Reputation: 4323
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.
Upvotes: 5
Reputation: 193
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.
Upvotes: 7