What is the difference between alert()
and window.alert()
functions? It seems to work the same.
Because window
is the global object, you can call an alert
either by it's shorthand: alert( 'Hello!' );
or by referencing the global object specifically: window.alert( 'Hello!' );
They are the same.
They are usually the same thing but, if in your scope, see example, the alert function got redefined then alert and window.alert will not be the same function.
(function () { function alert(test) { document.write(test); } alert("hello page"); window.alert("hello world"); })()
Hope the example will shed more light on this subject than my explanation.
You can also shadow the function name with a variable and obtain an error when calling it.
(function () { var alert; alert("Why don't you work, silly function?"); })()
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