lin*_*e-o 47 javascript debugging console internet-explorer-9
看起来我已经重新发明了这个轮子,但不知怎的,这在Internet Explorer 9中不起作用,但在IE6中也是如此.
function debug()
if(!window.console) {
window.console = { log: function() { /* do something */ } };
}
console.log.apply(console, arguments);
}
Run Code Online (Sandbox Code Playgroud)
F12 Debugger告诉我这个"对象"(console.log)不支持'apply'方法.它甚至不被认为是一种功能吗?还有其他指示或想法吗?
And*_*y E 93
我最近给出的答案的第二部分也回答了这个问题.我不认为这是一个副本,所以,为方便起见,我将它贴在这里:
控制台对象不是任何标准的一部分,是文档对象模型的扩展.与其他DOM对象一样,它被认为是一个宿主对象,不需要从Object继承,也不需要从Function继承它的方法,就像本机ECMAScript函数和对象一样.这就是申请和调用未定义的原因.在IE 9中,大多数DOM对象都得到了改进,可以从本机ECMAScript类型继承.由于开发人员工具被认为是IE的扩展(虽然是内置扩展),但他们显然没有得到与其他DOM相同的改进.
对于它的价值,您仍然可以在控制台方法上使用一些具有一点bind()魔法的Function.prototype方法:
Run Code Online (Sandbox Code Playgroud)var log = Function.prototype.bind.call(console.log, console); log.apply(console, ["this", "is", "a", "test"]); //-> "thisisatest"
所以你可以用同样的方式修复IE 9的所有console方法:
if (Function.prototype.bind && window.console && typeof console.log == "object"){
[
"log","info","warn","error","assert","dir","clear","profile","profileEnd"
].forEach(function (method) {
console[method] = this.bind(console[method], console);
}, Function.prototype.call);
}
Run Code Online (Sandbox Code Playgroud)
这将"主机"功能替换为调用"主机"功能的本机函数.您可以通过兼容的实现为得到它在Internet Explorer 8的工作Function.prototype.bind,并Array.prototype.forEach在你的代码,或重写上面的代码中纳入由这些方法所使用的技术.
console.logtypeof是"对象"而不是"功能" - Microsoft Connect(需要真实帐户)还有Paul Irish的做法.它比上面的一些答案更简单,但是使得log总是输出一个数组(即使只传入了一个参数):
// usage: log('inside coolFunc',this,arguments);
// http://paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function(){
log.history = log.history || []; // store logs to an array for reference
log.history.push(arguments);
if(this.console){
console.log( Array.prototype.slice.call(arguments) );
}
};
Run Code Online (Sandbox Code Playgroud)