132

If you run this in the chrome console:

console.log.apply(null, [array])

Chrome gives you back an error:

// TypeError: Illegal Invocation

Why? (Tested on Chrome 15 via OSX)

0

1 Answer 1

182

It may not work in cases when execution context changed from console to any other object:

This is expected because console.info expects its "this" reference to be console, not window.

console.info("stuff")
stuff
undefined
console.info.call(this, "stuff")
TypeError: Illegal invocation
console.info.call(console, "stuff")
stuff
undefined

This behavior is expected.

https://bugs.chromium.org/p/chromium/issues/detail?id=48662

5
  • 25
    If you need to use as a function, you can use console.info.bind(console)
    – C B
    Commented Jan 15, 2014 at 5:20
  • 3
    so can you use console.info.call(console, "stuff") in all browsers that support ES5?
    – mucaho
    Commented Jul 22, 2015 at 11:44
  • 2
    Same goes for apply: console.info.apply(console, arguments)
    – PeterM
    Commented Apr 21, 2016 at 7:51
  • Same argument applies to other functions such as console.log() and document.writeln(). So, always provide the correct execution context if using call() or apply(). Alternately, use bind() as @JohnWilliams has pointed out.
    – Alan C. S.
    Commented Jun 29, 2016 at 20:39
  • 1
    This is still applicable to IE11/Edge when the DevTools F12 are not open. Commented Jun 19, 2017 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.