From https://developer.mozilla.org/en-US/docs/Web/API/Window
The window object is the window itself.
Thus, all the variables and functions that you call are enclosed inside the object window. However, you can omit the reference to the object each time you call a function or variable.
Why is this? Think of a page with 2 or more frames. Each frame has its own window . You can access a variable inside a frame from another frame simply by accessing the window object of the object. This is true for every variable or function declared as global ... and it is also valid for built-in functions such as setTimeout .
So why sometimes we need to write a window.setTimeout explication?
Simply, if you are inside the scope and use the same name for your own function, you can choose which function to use.
eg:
function myF() { function setTimeout(callback,seconds) { // call the native setTimeout function return window.setTimeout(callback,seconds*1000); } // call your own setTimeout function (with seconds instead of milliseconds) setTimeout(function() {console.log("hi"); },3); } myF();
Note that the window object only exists in the browser environment. The global Node.js object is global , where window not defined.
Luca rainone
source share