Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any repercussions to storing globals like this?

So I know polluting the global namespace referencing the window is a bad thing, especially if you have multiple 3rd party references. So this would be not desirable:

window.someObject = someObject;

That will reference it everywhere. What if I instead use it like this?

var MyApplication = window.MyApplication;
MyApplication.someObject = someObject;

Of course using this approach requires referencing MyApplication = window.MyApplication at the top of each module that needs access to this created namespace. So back to my question. Is this an acceptable approach to giving global access without polluting the window global namespace?

like image 825
Joshua Avatar asked Jan 01 '26 04:01

Joshua


1 Answers

If you want global access, you need to have a global of some sort. This is the common way of doing it.

An example from the jQuery source code:

_jQuery = window.jQuery,
_$ = window.$,

I believe all of the big frameworks do it this way. I'd consider it perfectly acceptable to pollute the global namespace with one container variable.

like image 198
Matt Bryant Avatar answered Jan 02 '26 17:01

Matt Bryant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!