简体   繁体   English

什么是window.console && console.log?

[英]What is window.console && console.log?

 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 ? 什么是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 通过这个脚本不能在IE(所有版本)上工作 - > IE只有在按F12后运行javascript

The rightside-expression will only get evaluated if the leftside-expression is truthy . 只有当truthy表达式truthy才会评估右侧表达式。 Thats how the logical AND operator works. 这就是逻辑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 console object might not be available on every browser (especially mobiles). 正如您可能猜到的那样,这种情况的常见模式,因为console对象可能并非在每个浏览器(尤其是移动设备)上都可用。

1.) What is window.console && console.log ? 1.)什么是window.console && console.log?

console.log refers to the console object used for debugging. console.log是指用于调试的控制台对象。 for firefox i use firebug for example. 对于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 不,它仅用于调试目的

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

The above code is just short for an if conditional statement. 上面的代码只是if条件语句的缩写。 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. 对于大多数浏览器,您可以按F-12打开浏览器调试控制台。 Chrome has a debug console built in. Firefox has an extension called FireBug that you can use. Chrome内置了调试控制台.Firefox有一个名为FireBug的扩展程序,您可以使用它。 Below is the equivalent statement without the '&&'. 下面是没有'&&'的等效语句。

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. 我更喜欢在我的javascript代码的开头添加以下代码,以便我不必在所有地方都有这些“if”语句。 It really saves space and removes potential errors. 它确实节省了空间并消除了潜在的错误。

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(){}}

Console.log is a logger for browser which logs the messages on browser console. Console.log是浏览器的记录器,它在浏览器控制台上记录消息。 EDIT: Console.log is not supported for lower versions of Internet Explorer 编辑:较低版本的Internet Explorer不支持Console.log

此条件用于防止IE上的错误...因为,不幸的是,在IE(版本8)中我们无法使用console.log(“”)....但是测试人员仍然在Chrome上查看日志...

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

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