I have a fairly large script that contains about 60 global variables. I’m thinking about using the namespace pattern to encapsulate my script and have only one global variable that references one object.
Even though this pattern is considered best practice, I’m also thinking about an alternative: storing the global variables inside the DOM, in hidden divs, and accessing them with $('#MyGlobalVar1').text(). Is this a good idea or not?
Thanks for your suggestions.
No, this is not a good idea.
It pollutes the DOM with non-semantic data, and is less efficient as well. That’s even worse than polluting the global JS namespace, and worse still, it only allows you to store strings.