[英]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环境中,实际上没有提供
window
和console
没有被遮蔽或重新分配。
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中已更改;由
let
, const
或class
创建的全局变量不是全局对象的属性)。 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(...)
表示:
console
starting with the current execution context, then the next one, then the next, until it finds it at global scope. console
的绑定,然后搜索下一个绑定,然后搜索下一个,直到它在全局范围内找到它。 log
property on the resulting object. log
属性。 window.console.log(...)
means: window.console.log(...)
表示:
window
starting with the current execution context, then the next one, then the next, until it finds it at global scope. window
的绑定,然后搜索下一个绑定,然后搜索下一个绑定,直到它在全局范围内找到它。 console
property on the resulting object. console
属性。 log
property on the resulting object. log
属性。 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.log
和window.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 simplyconsole
.它作为
Window.console
公开,可以简单地作为console
引用。
Adding to this, the question may have also been- 除此之外,问题可能还有 -
What's the difference between console.log
and window.console
. console.log
和window.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.