如何组织复杂的jquery应用程序?
所以我的 jquery 应用程序完全有问题。当应用程序很小时,这很酷。但当它相当大时,那就很混乱了。嵌套函数、ajax 调用、选择器、dom 操作。看起来它根本无法容纳,尤其是当它具有复杂的逻辑时。
函数式方法对我帮助不大。我不知道如何在实践中使用类和继承。如何组织代码?我读过很多关于原型继承和伪经典继承的文章,但它们只是解释了事情是如何工作的,比如如何从“人类”继承“人”或类似的东西。我怎样才能在现实生活中真正使用它?
So I have a problem with my jquery applications entirely. When the application is small it's cool. But when it is quite large it is such a mess.. Nested functions, ajax calls, selectors, dom manipulations. Seems like it's non-containable at all, especially when it has a complex logic.
The functional approach does not help me out much. And I don't see how to use classes and inheritance here in practice. How to organize the code? I have read a lot of articles about prototypical and pseudo classical inheritance but they just explain how things works, like how you can inherit 'Person' from 'Human' or something like this. How can I actually use it in real life?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现有两件事确实有助于组织 javascript。一种使用对象进行封装( http://www.dustindiaz.com/namespace-your-javascript ),并且二、为常见任务编写一个库。正常的重构策略适用于 javascript,提取函数,通用化,如果您有 4 个函数做几乎相同的事情,请将它们更改为可以处理所有四种情况的单个函数。
I find two things that really help organize javascript. One use objects for encapsulation ( http://www.dustindiaz.com/namespace-your-javascript ), and two, write a library for common tasks. The normal refactoring strategies work for javascript, extract functions, commonize, if you have 4 functions doing nearly the same thing, change them to a single function which can handle all four cases.
在规划我目前正在开发的大型 jQuery 应用程序时,我发现了这篇文章 Addy Osmani 的文章非常有用。
不过,要全面披露的是:我们最终将大部分应用程序主干挂在 YUI3 核心上。我们所有的“控制器”代码都是用 jQuery 编写的,但前端数据模型是 YUI3。
When planning a large jQuery application that I'm currently working on, I found this post by Addy Osmani very useful.
Full disclosure, though: we ended up hanging most of our application backbone off of a YUI3 core. All our "controller" code is written in jQuery, but the frontend data model is YUI3.