Why does console.log.apply() throw an Illegal Invocation error?
23,138
Here's an alternate solution. I'm not sure the case where there are no args works as expected.
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')
Author by
joshuapoehls
Updated on July 09, 2022Comments
-
joshuapoehls almost 2 years
When I execute the following code in Chrome 18 beta I get the error:
console.log.apply(this, ['message']);
TypeError: Illegal invocation.
In Firefox 10 it works as expected.
In IE9 I get the error:
Object doesn't support property or method 'apply'
.I'm guessing this has to do with how the browser has implemented
console.log
.Why does it work in Firefox but not in Chrome and IE? I'm hoping someone can shed some light on the cause of this and its ramifications.
Here is an executable sample on JS Bin.