Var cl = console.log not working

I did this in my Angular application:

var cl = console.log;
cl(123);

however, I got the following error message:

Uncaught TypeError: Illegal invocation

This happened in Chrome. He works at Nodejs.

I'm confused. Is this an illegal code?

+4
source share
1 answer

clonly refers to a method log(). log()expects consoleas a context, but receives window. To solve, bind consoleas a context:

var cl = console.log.bind(console);
cl("Hello");
Run codeHide result
+5
source

All Articles