Illegal Invocation error when console.log passed in a function

后端 未结 1 1212
谎友^
谎友^ 2020-12-01 22:40

I am bit confused over this. Please find the code as below.

var o={
 printToConsole: function(f){
   f(1);
}
};

o.printToConsole(console.log);
相关标签:
1条回答
  • 2020-12-01 22:45

    Change

    o.printToConsole(console.log);
    

    to

    o.printToConsole(console.log.bind(console));
    

    or

    o.printToConsole(function(){ console.log.apply(console.log, arguments) });
    

    The console.log function only works when the receiver (this) is the console (in fact, it's browser dependent).

    0 讨论(0)
提交回复
热议问题