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)

randers
  • 5,031
  • 5
  • 37
  • 64
Jacksonkr
  • 31,583
  • 39
  • 180
  • 284

1 Answers1

181

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Pavel Podlipensky
  • 8,201
  • 5
  • 42
  • 53
  • 25
    If you need to use as a function, you can use console.info.bind(console) – C B Jan 15 '14 at 05:20
  • 3
    so can you use `console.info.call(console, "stuff")` in all browsers that support ES5? – mucaho Jul 22 '15 at 11:44
  • 2
    Same goes for apply: `console.info.apply(console, arguments)` – PeterM Apr 21 '16 at 07: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. Jun 29 '16 at 20:39
  • 1
    This is still applicable to IE11/Edge when the DevTools F12 are not open. – Benny Bottema Jun 19 '17 at 15:42