Is console.log a regular object?

Possible duplicate:
If Javascript has first-class features, why doesn't it work?

The following message appears in Chrome: Uncaught TypeError: Illegal invocation:

g = console.log; g(1); 

Why is this happening, and why can't I treat console.log as a regular object?

+4
source share
1 answer

This is because you have lost the console link. You simply call log directly, without context. You can call the function in the console context to make it work:

 g.call(console, 1); 

Or, so that you cannot do this every time, you can bind a function to a console object:

 var g = console.log.bind(console); 

References

+8
source

Source: https://habr.com/ru/post/1442361/


All Articles