为什么}!{在JavaScript控制台中导致False? [英] Why does }!{ result in False in a JavaScript console?
问题描述
如果将}!{
放在Chrome的JavaScript控制台中,结果将得到 false
.
If you put }!{
in your JavaScript console in Chrome, as a result you will get false
.
为什么我们没有收到错误消息?
Why don't we get an error?
推荐答案
此行为的原因是因为Chrome使用另一段代码将您输入到控制台中的所有内容包装起来.
The reason for this behaviour is because Chrome wraps anything you enter into the console with another piece of code.
(在撰写本文时)包装的代码如下:
The code it wraps with (at the time of writing) is as follows:
with ((window && window.console && window.console._commandLineAPI) || {}) {
// Your code here.
}
输入}!{
将关闭代码块大括号,并在最后创建一个新的(取反)对象.
Entering }!{
closes the code block brace, and makes a new (negated) object at the end.
您可以在控制台中看到自己的!{}
返回 false
.
As you can see for youself in the console !{}
returns false
.
我经过一个漫长的调查过程来找到答案,下面保留了我的原始评论
I went through quite a lengthy investigative process to find the answer to this, my original comments are preserved below
原始答案:
只是一种理论;我想象在控制台中输入的代码在函数内部被调用
Just a theory; I imagine the code entered in the console being called inside a function
函数execUserCode(){代码}
您正在做的是创建
function execUserCode(){}!{}
控制台返回的最后一个结果实际上是!{}
= false
The console is returning the last result, which is actually !{ }
= false
有关此怎么可能是错误的许多评论.我同意.这只是一个理论.
Lots of comments about how this is probably wrong. I agree. It's just a theory.
我喜欢这些谜题,所以我对Chromium来源进行了深入的研究,这对我来说有点多了,但是我会留下一些建议,以防其他人被刺伤.
I like these kind of puzzles so I had a dig through the Chromium source, it's a bit much for me but I'll leave some pointers in case anyone else has a stab.
JS控制台称为检查器",可以在这里找到:
The JS console is called the "inspector" and can be found here:
chromium/src/third_party/WebKit/Source/WebCore/inspector/
我一直在查看 inspector/front-end/ConsoleView.js
,我想我发现了执行用户代码的位置.
I've been looking at inspector/front-end/ConsoleView.js
and I think I found a bit of where the user code is executed.
evaluateUsingTextPrompt: function(expression, showResultOnly)
{
this._appendCommand(expression, this.prompt.text, false, showResultOnly);
},
原因!
小脑波.我是在控制台中完成的
The reason!
Little brainwave. I did this in the console
> myEval = eval
> eval = function(str) { console.log(str); myEval(str) }
> }!{
结果:
with ((window && window.console && window.console._commandLineAPI) || {}) {
}!{
}
我很近,但是现在我们有了答案:)
I was close, but now we have the answer :)
该代码是在当前第440行附近的 chromium/src/third_party/WebKit/Source/WebCore/inspector/InjectedScriptSource.js
中生成的.
The code is generated in chromium/src/third_party/WebKit/Source/WebCore/inspector/InjectedScriptSource.js
currently around line 440.
这篇关于为什么}!{在JavaScript控制台中导致False?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!