Javascript - Override console.log and keep the old function

前端 未结 8 1195
春和景丽
春和景丽 2021-01-12 08:49

I would like to override console.log and call it on my new function.

I try something like this but I get Uncaught TypeError: Illegal invocation:

8条回答
  •  星月不相逢
    2021-01-12 09:39

    You are invoking the console.log function on the window object. You should instead invoke it on the console object.

    console.log = function() {
      this.apply(console, arguments);
    }.bind(console.log);
    
    console.log('as');
    

提交回复
热议问题