Abraham Adam
Abraham Adam

Reputation: 633

console functions saved to variable in chrome

This seems to happen only chrome, wondering if anyone know why it happens or how to fix it.

doing console.log('test') print out 'test'

now if we save the function

var log = console.log
log('test')

this gives us TypeError: Illegal invocation

same thing happens with all the methods of console, like error and warn

Upvotes: 0

Views: 92

Answers (1)

robertklep
robertklep

Reputation: 203369

Try this:

var log = console.log.bind(console);
log('test');

With .bind you'll provide console with the correct context (i.e. itself) to operate.

Upvotes: 2

Related Questions