DEV Community

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

Posted on

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

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 (5)

Collapse
tomeraitz profile image
Tomer Raitz

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"

Collapse
uzair004 profile image
Muhammad Uzair

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.

Collapse
derder56 profile image
DerDer56 Author

Thanks for the explanation! Very useful! :)

Collapse
justcoding profile image
hello936 • Edited

I always write window. before anything

Collapse
dapepsi profile image
Daniel Meier

window.before window.anything