Reputation: 105517
I have a page with one external script file. Inside this script 3 global objects are used:
var oArticle = new Article();
var oUiManager = new UiManager();
var oController = new Controller();
I was suggested today to put them all into one object, for example inside exercisePage:
var exercisePage = {
"oArticle": new Article(),
"oUiManager": new UiManager(),
"oController": new Controller()
};
What are the benefits of such approach if I'm using only one script file for the page?
Upvotes: 0
Views: 57
Reputation: 77786
Grouping objects like this just minimizes the chance for global variable conflicts.
If you're including a lot of external libs, you'll want be certain they all work without stepping on each other's variables.
That said, most libs are designed to play nice and they would be unlikely to collide with user-defined variables.
Better than putting them in an object though, an anonymous closure would be even more effective
(function(){
var oArticle = new Article();
var oUiManager = new UiManager();
var oController = new Controller();
// other code that uses these objects
})();
Upvotes: 2