在全局数组中包含数千个对象是否实用?
假设我在 JS 中有一个名为 App
的全局数组。
var App = {};
现在,我们构建了数十种表单。每个表单可以有数十个对象(按钮、网格、面板、标签等)。
另外,我们的应用程序具有不同的范围,例如工作订单、租赁等。
那么,可以安全地假设我可以保持一切干净,比如:
var App = {};
App.Leases = {};
App.WorkOrders = {};
App.Leases.myGrid = Ext.grid.GridPanel({});
App.Leases.btnNewButton = Ext.Button({});
App.WorkOrders.searchGrid = Ext.grid.GridPanel({});
这是否太过分了?我正在启动一个新的应用程序,它将变得相当大,我们正在寻找新的方法来保持事物井井有条(与以前的项目相比)。
您对这样的技术有何看法?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据我的理解,这对于跟踪数据并使内容井井有条来说非常巧妙。
当您计算复杂的架构时,您应该关注的是对 DOM 的影响,因为这会减慢页面渲染和交互(特别是使用旧浏览器)。请记住,声明全局 var App = {};将在窗口对象树中创建一个新项目。
话虽这么说,我将考虑三个要点:
仅在需要时全局存储数据,而不是控制引用(再次根据我的理解,存储控制引用是没有意义的......如果您仍然需要跟踪,您应该考虑存储控件 id )
使用这种全局模式存储控件的风险可能是由于循环引用而导致内存泄漏......所以也是如此帮自己一个忙,在这样做之前请三思而后行。
不要加载可能不会使用的内容,而是更喜欢延迟加载...这意味着按需加载一次内容,然后根据需要缓存加载的数据。
您应该在开发时查看JSLint,以免最终出现大惊喜: )
希望这有帮助
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