Cha*_*les 7 javascript console overriding
我想覆盖console.log并在我的新函数上调用它.
我尝试这样的东西,但我得到Uncaught TypeError: Illegal invocation:
console.log = function() {
this.apply(window, arguments);
}.bind(console.log);
console.log('as');
Run Code Online (Sandbox Code Playgroud)
这是我的目标:
console.error(exception); 应该做:
console.error = function() {
if (typeof exception.stack !== 'undefined') {
console.error(exception.stack);
} else {
console.error.apply(windows, arguments);
}
}.bind(console.error);
console.error('as');
console.error({stack: 'my stack....'});
Run Code Online (Sandbox Code Playgroud)
编辑:实际上,它适用于Firefox而不是Chrome ... 这是Chrome中的一个错误:https://code.google.com/p/chromium/issues/detail?id = 167911
你可以有类似的东西:
console.error = (function() {
var error = console.error;
return function(exception) {
if (typeof exception.stack !== 'undefined') {
error.call(console, exception.stack);
} else {
error.apply(console, arguments);
}
}
})();
Run Code Online (Sandbox Code Playgroud)
实现您想要的最佳方式:
// define a new console
var console=(function(oldCons){
return {
log: function(text){
oldCons.log(text);
// Your code
},
info: function (text) {
oldCons.info(text);
// Your code
},
warn: function (text) {
oldCons.warn(text);
// Your code
},
error: function (text) {
oldCons.error(text);
// Your code
}
};
}(window.console));
//Then redefine the old console
window.console = console;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8567 次 |
| 最近记录: |