Skip to content

Instantly share code, notes, and snippets.

@euank
Last active July 11, 2021 02:54
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save euank/7523581 to your computer and use it in GitHub Desktop.
Save euank/7523581 to your computer and use it in GitHub Desktop.
Fix chrome's console.log 'TypeError: Illegal invocation'

Chrome exhibits behavior different from firefox (and other javascript environments) in that console.log requires "this" to be console. This issue is discussed here.

Simple failing code is

var helloWorld = function(callback) { 
  callback("Hello World"); 
}; 
helloWorld(console.log);

The included snippet makes failing code function correctly without any other changes required. It just has to be run before any console.logs. Similar code can be written for console.info and so on.

(function(){ var c = window.console.log; window.console.log = function() {
c.apply(window.console, arguments); }})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment