什么是窗口。控制台& & console.log吗?

时间:2021-05-08 00:26:37
 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

什么是窗口。控制台& &控制台。日志吗?它必须在代码中吗?通过这个脚本,IE(所有版本)无法运行——> IE仅在按下F12后才能运行javascript

5 个解决方案

#1


17  

The rightside-expression will only get evaluated if the leftside-expression is truthy. Thats how the logical AND operator works.

只有当左边的表达式是真实的时,右边的表达式才会被计算。这就是逻辑运算符和运算符的工作原理。

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).

正如您可能猜到的那样,这是这种情况的常见模式,因为控制台对象可能不在每个浏览器(特别是移动设备)上可用。

#2


4  

1.) What is window.console && console.log ?

1)。什么是窗口。控制台& &控制台。日志吗?

console.log refers to the console object used for debugging. for firefox i use firebug for example.

控制台。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.

但是如果控制台不可用,脚本将崩溃。所以窗口。控制台检查控制台对象是否存在,如果存在,它使用日志函数打印一些调试信息。

2.) Does it have to be in the code?

2)。它必须在代码中吗?

no, its only for debugging purpose

不,它只用于调试

#3


3  

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

#4


1  

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

控制台。log是一个浏览器的日志记录器,它在浏览器控制台记录消息。编辑:控制台。较低版本的Internet Explorer不支持日志

#5


0  

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(version 8)我们不能使用console.log(" ")....但是测试人员仍然会查看Chrome上的日志……

#1


17  

The rightside-expression will only get evaluated if the leftside-expression is truthy. Thats how the logical AND operator works.

只有当左边的表达式是真实的时,右边的表达式才会被计算。这就是逻辑运算符和运算符的工作原理。

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).

正如您可能猜到的那样,这是这种情况的常见模式,因为控制台对象可能不在每个浏览器(特别是移动设备)上可用。

#2


4  

1.) What is window.console && console.log ?

1)。什么是窗口。控制台& &控制台。日志吗?

console.log refers to the console object used for debugging. for firefox i use firebug for example.

控制台。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.

但是如果控制台不可用,脚本将崩溃。所以窗口。控制台检查控制台对象是否存在,如果存在,它使用日志函数打印一些调试信息。

2.) Does it have to be in the code?

2)。它必须在代码中吗?

no, its only for debugging purpose

不,它只用于调试

#3


3  

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

#4


1  

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

控制台。log是一个浏览器的日志记录器,它在浏览器控制台记录消息。编辑:控制台。较低版本的Internet Explorer不支持日志

#5


0  

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(version 8)我们不能使用console.log(" ")....但是测试人员仍然会查看Chrome上的日志……