loading...
Cover image for Why do people write "window." in front of some things and not others?

Why do people write "window." in front of some things and not others?

derder56 profile image DerDer56 ・1 min read

Why do some people write:

  • window.open()
  • window.innerHeight
  • window.scrollY etc.

But not:

  • window.document
  • window.alert
  • window.location etc?

Any answers are appreciated.

Discussion

markdown guide
 

I think it's always a good idea to use window because of the risk of an override. for example:

let alert = function(string){console.log(string)}

if you do alert ("hello") it will console "hello" but if you will do window.alert("hello") it will pop up the alert with "hello"

 

Window is like + with positive numbers, it's there but you can't see it :D ( old school math logic).

In browser we are working in global environment called window. So there are many methods, properties provided by "window object" and as you may know we access object properties using dot(.) notation. But since we are already inside object (window) we necessarily don't need to say , access property on Window.

One of the reason we use window with some properties is because those same properties are also defined in other objects then window , so it may give us those.

For instance, scrollY is also available on "documentElement" object, and others too.

So avoid confusing by mentioning which one you are referring to. You can always use window before any window property or method.

 

Thanks for the explanation! Very useful! :)

 

I always write window. before anything

 

window.before window.anything