The below code returns a pop-up window with 'hello'.
alert.call(this, 'hello');
But the below code returns an error "TypeError: Illegal invocation".
console.log.call(this, 'hello');
What is the difference in the implements of alert and console.log?
alert
is a global method (window.alert
). If you call it alert.call(this)
, this
is the window object.
Because log is a method in the console object, it expects this
to be the console object itself, but you are still calling it with this
(window
), so you get an error.
Running console.log.call(console, 'test')
will work fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With