简体   繁体   English

window.console.log和console.log之间有什么区别

[英]What is the difference between window.console.log and console.log

Just went through an interview. 刚接受采访。 The first question asked me was what is console.log() . 第一个问题问我是什么是console.log() I answered with so confidence. 我非常自信地回答。 Again, 再次,

The second question was, what is the difference between window.console.log() and console.log() . 第二个问题是, window.console.log()console.log()之间有什么区别。 I was speechless. 我无言以对。 Tried searching in Google and Stack Overflow. 尝试在Google和Stack Overflow中搜索。 I didn't find such helpful post to understand the difference between them. 我没有找到这样有用的帖子来理解它们之间的区别。

Any thoughts is greatly appreciated. 任何想法都非常感谢。

In a normal browser context, there is no difference. 在普通的浏览器环境中,没有区别。 console is a global variable, and all globals are properties of the window object. console是一个全局变量,所有全局变量都是window对象的属性。

 console.log(console.log==window.console.log) // true 

There are some caveats, such as when not running in the browser, or if the console variable has been reassigned. 有一些注意事项,例如未在浏览器中运行,或者是否已重新分配控制台变量。 TJ Crowder explains it nicely. TJ Crowder很好地解释了这一点。

If you mean in the default browser JavaScript environment, there is effectively none provided window and console haven't been shadowed or reassigned. 如果您的意思是在默认的浏览器JavaScript环境中,实际上没有提供 windowconsole没有被遮蔽或重新分配。

In the default browser JavaScript environment, window is a global that refers to the global object, which is also the window object. 在默认浏览器JavaScript环境中, window是一个全局对象,它引用全局对象,也是窗口对象。 The global object holds most globals as properties (it used to be all, but in ES2015 that changed; globals created by let , const , or class are not properties of the global object). 全局对象将大多数全局变量保存为属性(它曾经是全部,但在ES2015中已更改;由letconstclass创建的全局变量不是全局对象的属性)。 But that's not true in most non-browser environments (NodeJS, for instance, uses global instead of window ), or even in some non-default browser environments (such as a web worker's environment, which doesn't have window as they can't access the window). 但是在大多数非浏览器环境中都不是这样(例如,NodeJS使用global而不是window ),甚至在某些非默认浏览器环境中(例如Web工作者的环境,它没有window因为它们可以'访问窗口)。 So in environments where window isn't defined, window.console.log will fail where console.log wouldn't (provided the environment provides a global console ). 因此,在未定义window环境中, window.console.log将失败,而console.log不会(如果环境提供全局console )。

To understand the difference, let's work each of them through: 为了理解这些差异,让我们通过以下方式工作:

console.log(...) means: console.log(...)表示:

  1. The JavaScript engine has to search for the a binding for the identifier console starting with the current execution context, then the next one, then the next, until it finds it at global scope. JavaScript引擎必须从当前执行上下文开始搜索标识符console的绑定,然后搜索下一个绑定,然后搜索下一个,直到它在全局范围内找到它。
  2. Then it looks up the log property on the resulting object. 然后它在结果对象上查找log属性。
  3. Then it calls it 然后它调用它

window.console.log(...) means: window.console.log(...)表示:

  1. The JavaScript engine has to search for the a binding for the identifier window starting with the current execution context, then the next one, then the next, until it finds it at global scope. JavaScript引擎必须从当前执行上下文开始搜索标识符window的绑定,然后搜索下一个绑定,然后搜索下一个绑定,直到它在全局范围内找到它。
  2. Then it looks up the console property on the resulting object. 然后它在结果对象上查找console属性。
  3. Then it looks up the log property on the resulting object. 然后它在结果对象上查找log属性。
  4. Then it calls it 然后它调用它

So for instance, here's an example where console has been shadowed, and so console.log fails whereas window.console.log works: 例如,这是一个console被遮蔽的示例,因此console.log失败而window.console.log工作:

 function foo() { var console = 42; try { console.log("You WON'T see this."); } catch (e) { } try { window.console.log("You WILL see this."); } catch (e) { } } foo(); 

There is no difference between console.log and window.console.log . console.logwindow.console.log之间没有区别。 Have a check on MDN . 检查MDN They clearly quote - 他们明确引用 -

The Console object can be accessed from any global object, Window on browsing scopes, WorkerGlobalScope, and its specific variants in workers via property console. 可以通过属性控制台从任何全局对象,浏览范围上的Window,WorkerGlobalScope及其在工作程序中的特定变体访问Console对象。 It's exposed as Window.console , and can be referenced as simply console . 它作为Window.console公开,可以简单地作为console引用。



Adding to this, the question may have also been- 除此之外,问题可能还有 -

What's the difference between console.log and window.console . console.logwindow.console之间有什么区别。

The answer for this would be- 对此的答案是 -

console.log is used for logging(as you know). console.log用于记录(如您所知)。

window.console checks if the console is available( truthy value) so that we can log next.(in case of mobile browsers, they don't support debugger/console) window.console检查控制台是否可用( truthy值),以便我们可以记录下一步。(在移动浏览器的情况下,它们不支持调试器/控制台)

Common pattern in code for this is- 代码中的常见模式是 -

window.console && console.log(open_date);

Which is basically short code for - 这基本上是短代码 -

if( window.console ) {
    console.log( open_date );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM