jos*_*hls 49 javascript firefox internet-explorer google-chrome
当我在Chrome 18 beta中执行以下代码时,我收到错误消息:
console.log.apply(this, ['message']);
Run Code Online (Sandbox Code Playgroud)
TypeError:非法调用.
在Firefox 10中,它按预期工作.
在IE9中我得到错误:Object doesn't support property or method 'apply'.
我猜这与浏览器的实现方式有关console.log.
为什么它在Firefox中有效但在Chrome和IE中无效?我希望有人可以了解这个及其后果的原因.
这是JS Bin上的可执行示例.
小智 105
console并且log是主机对象.它们的行为依赖于实现,并且在很大程度上不需要实现ECMAScript的语义.
FWIW,您的jsBin也会在Chrome中失败,除非您将其更改为...
console.log.apply(console, ['message']);
Run Code Online (Sandbox Code Playgroud)
但这似乎log只是预测了一个呼唤的背景console.
jim*_*ont 11
这是另一种解决方案.我不确定没有args按预期工作的情况.
function logr(){
var i = -1, l = arguments.length, args = [], fn = 'console.log(args)';
while(++i<l){
args.push('args['+i+']');
};
fn = new Function('args',fn.replace(/args/,args.join(',')));
fn(arguments);
};
logr(1,2,3);
logr();
logr({},this,'done')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21233 次 |
| 最近记录: |