javascript - When is it okay to attach properties to window? -


While browsing, I often take a look at the source code of the web page to see how some things were received. Are there.

Once I see that there is a thing like: window.myApp.apiKey = 12345; or window.myApp.welcomeMsg = "Hello there!";

I was always under the impression that this is bad practice to attach the properties of window object (as it has been explained), but it seems that myApp A type of name is used as a space & amp; Since this is often a brand name, it is unlikely to struggle with anything.

Still - how is it so okay, there are no indicators for this? (That is, when I know that 3G libraries will not be used in the project, which can change rapidly in real life).

In general, I use the following approach ...

 < Code> if (window of type ['myApp'] == 'undefined') {Warning ('OK.');} Else {Warning ('Error: added original API, AJAX message server, email you'.) ;}  

... Of course, whether you should do it is subjective, I personally use ...

  var option = new function () {This.name = '';} option. Whatever = 'this string'; Warning (option.whatever); // 'this string'  

... but I can rename it and modify it in the future as I do with all my code.

As Engo Burke notes in the comment, in the end it seems that everything is an object of a window object, the only difference between those objects is the context and How you can interact with them (like you can not delete the object defined by var , however you can delete window.myObject When it comes to window.myObject = 1; .

for more in depth information.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -