Javascript window.console && console.log 是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14561462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is window.console && console.log?
提问by user2018036
if (open_date) {
open_date = get_date_from_string(open_date);
window.console && console.log(open_date);
window.console && console.log(cancel_until);
What is window.console && console.log? Does it have to be in the code? Through this script does not work on IE (all version) --> IE runs javascript only after pressing F12
什么是window.console && console.log?它必须在代码中吗?通过此脚本不适用于 IE(所有版本)--> IE 仅在按 F12 后运行 javascript
回答by jAndy
The rightside-expression will only get evaluated if the leftside-expression is truthy. Thats how the logical ANDoperator works.
仅当左侧表达式为 时才会对右侧表达式求值truthy。这就是逻辑 AND运算符的工作原理。
Its basically short for
它基本上是简称
if( window.console ) {
console.log( open_date );
}
As you might guess correctly, it's a common pattern for this case, because the consoleobject might not be available on every browser (especially mobiles).
您可能猜对了,这是这种情况的常见模式,因为该console对象可能并非在每个浏览器(尤其是移动设备)上都可用。
回答by Jakob
1.) What is window.console && console.log ?
1.) 什么是 window.console && console.log ?
console.log refers to the console object used for debugging. for firefox i use firebugfor example.
console.log 是指用于调试的控制台对象。例如,对于 firefox,我使用firebug。
but if the console is not available the script will crash. so window.console checks if the console object is there and if so it uses its log function to print out some debug information.
但如果控制台不可用,脚本就会崩溃。所以 window.console 检查控制台对象是否存在,如果存在,它使用它的日志功能打印出一些调试信息。
2.) Does it have to be in the code?
2.) 它必须在代码中吗?
no, its only for debugging purpose
不,它仅用于调试目的
回答by Abraham
window.console && console.log(open_date);
The above code is just short for an if conditional statement. It doesn't have to be there. it is for debugging purposes. For most browsers, you can hit F-12 to open the browser debug console. Chrome has a debug console built in. Firefox has an extension called FireBug that you can use. Below is the equivalent statement without the '&&'.
上面的代码只是 if 条件语句的缩写。它不必在那里。它用于调试目的。对于大多数浏览器,您可以按 F-12 打开浏览器调试控制台。Chrome 有一个内置的调试控制台。Firefox 有一个名为 FireBug 的扩展程序,您可以使用它。下面是不带“&&”的等效语句。
if (window.console) console.log(open_date);
I prefer to add the following code at the beginning of my javascript code so that I do not have to have these "if" statements all over the place. It really saves space and removes potential errors.
我更喜欢在我的 javascript 代码的开头添加以下代码,这样我就不必到处都有这些“if”语句。它确实节省了空间并消除了潜在的错误。
if (typeof console == "undefined") { window.console = {log: function() {}}; }
Jon Dvorak's comment above contains an elegant alternative way of doing this:
上面 Jon Dvorak 的评论包含了一种优雅的替代方法:
console = window.console || {log:function(){}}
回答by Abhijeet Pawar
Console.log is a logger for browser which logs the messages on browser console. EDIT: Console.log is not supported for lower versions of Internet Explorer
Console.log 是浏览器的记录器,用于在浏览器控制台上记录消息。编辑:较低版本的 Internet Explorer 不支持 Console.log
回答by Mohamed Taboubi
This condition is used to prevent errors on IE ... because, unfortunately, in IE (version 8) we cannot use console.log("") .... however testers still view the logs on Chrome ...
这个条件用于防止 IE 上的错误......因为,不幸的是,在 IE(版本 8)中我们不能使用 console.log("") .... 但是测试人员仍然在 Chrome 上查看日志......

