Is it practical to have thousands of objects inside a global array?
Let's say I have a global array in JS called App
.
var App = {};
Now, we build dozens of forms. Each form could have dozens of objects (buttons, grids, panels, labels, etc).
Plus, our application has different scopes like WorkOrders, Leases, etc
.
So, would it be safe to assume I could keep everything clean like:
var App = {};
App.Leases = {};
App.WorkOrders = {};
App.Leases.myGrid = Ext.grid.GridPanel({});
App.Leases.btnNewButton = Ext.Button({});
App.WorkOrders.searchGrid = Ext.grid.GridPanel({});
Is this overkill? I'm starting a new app that will grow pretty large and we are looking for new ways to keep things organized (compared to previous projects).
What are your thoughts on such开发者_运维技巧 a technique?
From my understanding, this is pretty neat to keep track of data, and to keep stuff well organized.
When you are computing complex architecture, what you should focus on, is the impact on the DOM, cause this is what can slow down your page rendering and interaction ( specially with old browsers ). Keep in mind that declaring a global var App = {}; will create a new item within the window object tree.
That being said, I 'll consider three main points :
only storing data globally if needed and not control references ( again from my understanding there is no point to store control reference ... if you still need to keep track you should consider storing the control id )
the risk of storing controls using this global pattern is probably to end with memory leaks due to circular references ... so do yourself a favor, and think about it twice before doing it this way.
don't load stuff that won't probably get used, but prefer lazy loading ... meaning load stuff once on demand, and then cache loaded data if you want/need to.
You should have a look at JSLint while developing in order not to have big surprises at the end :)
Hope this helps
精彩评论