Do I need to omit the word "window" when calling a function of the global object?

Asked

Viewed 60 times

2

Following good practice, leaving code readable to other devs, which I should adopt when I call object methods window? Omit or not?

Some examples (simple and isolated):

const buttonStyles = getComputedStyle(button);
//const buttonStyles = window.getComputedStyle(button);

alert(erroMessage);
//window.alert(erroMessage);

onload(funtion(){});
//window.onload(funtion(){});

2 answers

2


This is not good practice. Readable code is subjective, and will only be good for others if you are doing what they expect. So no one can say whether to omit or not, depending on what the team has established. What I see most is people omitting, but the most important thing is to be consistent.

If you solve do not omit you need to justify it. It is because it is clearer that they are object methods window? I do not know if it is a valid argument because this is an object that everyone should know by heart and understand that they are of this object even if they do not have a prefix indicating so. It’s that thing of memorizing the obvious. Readability would happen with objects that are not patterns.

  • I believed that this omission or not of the window object was related to good bráticas, but it does not have. Thanks for the answer, Maniero.

  • @Felipeoliveira has almost nothing to do with good practices. Look here at the site what I talk about the subject.

  • Sure! I’ll read it. Thanks again.

0

The use of obvious form tends to be better understood, however it must be revealed what was defined in team, if indeed something was defined.

If not, or even in general situations, it is very recommended to standardize the code among the team members, so as word finally strongly recommend the use of Linters (like the ESLint or TSLint, to the TypeScript specifically).

  • 1

    Thank you for your reply, Leandro!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.