Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between alert and window.alert?

Tags:

javascript

What is the difference between alert() and window.alert() functions? It seems to work the same.

like image 755
scdmb Avatar asked Nov 19 '12 18:11

scdmb


2 Answers

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.

like image 196
Kevin Boucher Avatar answered Sep 19 '22 23:09

Kevin Boucher


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?"); })() 
like image 35
Eineki Avatar answered Sep 20 '22 23:09

Eineki