定义一个新的console.log并取消激活原来的console.log

Define a new console.log and desactivate the orginal one

本文关键字:log console 取消 原来 激活 一个 定义      更新时间:2023-09-26

我想创建一个与console.log完全相同的变量,并停用原始的console.log。

newLog('hello world'); // Should be displayed
console.log('hello world'); // Should be ignored

我找到了两种方法:

第一个

:

var newLog = console.log;
console.log = function() {};
第二:

var newLog = console.log.bind(console);
console.log = function() {};

第一个可以使用IE9,但不能使用Chrome。第二个可以在Chrome上使用,但不能在IE9上使用。我没有在其他浏览器上试过。

有没有一种方法可以让所有浏览器一起工作而不混合解决方案?

使用window.navigator对象找出浏览器当前是什么,并将其与if else语句结合使用

if(isChrome) {
   //execute chrome specific code
} else if(isIE) {
   //execute IE code
}