面向对象的 JavaScript 不一定能解决所有问题。
我的建议是谨慎选择你在这个主题上的选择。
在实践中,OO-JS 可以为您的代码增加更多复杂性,以便尝试更像传统的面向对象语言。您可能知道,JS 是独一无二的。
重要的是要知道有一些设计模式可以构建您的代码并保持实现的轻量和灵活。
我看到构建高级 JS 实现的是设计模式,而不是 OO。套用 Axel Rauchmeyer 的话说——“面向对象的方法不适合基本的 JavaScript 语法,它是一种扭曲和扭曲的实现,没有它,JS 的表现力要强得多。”
这种分析的根源归结为JS没有类。本质上,由于一切都是对象,因此您已经拥有面向对象的变量和函数。因此,该问题与编译语言 (C/Java) 中的问题略有不同。
JavaScript 有哪些设计模式?
一个很好的检查资源是 Addy O' Somani 和 Essential Design Patterns。
他写了这本关于 JavaScript 中的设计模式的书。
但还有更多……更多。
A. require.js - 有一种以令人印象深刻的方式加载 JS 代码模块的方法。这些通常被称为模块加载器,并且被广泛认为是加载 js 文件的未来,因为它们优化了运行时的性能。yepnope 和其他人存在。如果您要加载多个 js 文件,请记住这一点。(根据要求移至顶部)。
B. MVC - There are dozens of Model View Controller frameworks to help you structure code.
It is a Pattern, but may be unreasonable for your purposes. You mentioned backbone, knockout and angular... Yes. These can do the trick for you, but I would be concerned that they might be 1) high learning-curve, and 2) overkill for your environment.
C. Namespace or Module Pattern. Are probably the most important for your needs.
To solve global variables just wrap them in a namespace and reference that.
These are very good patterns that give rise to module loaders.
D. Closure - You mentioned OO JS. On piece of this that is useful is the notion of closures to provide yourself with... private members. At first this is a cryptic idea, but after you recognize the pattern, it is trivial practice.
E. Custom Events - It becomes very important to not use hard references between objects. Example: AnotherObject.member;
This is because it will tightly couple the two objects together and make both of them inflexible to change. To solve this, trigger and listen to events. In traditional Design Patterns this is the Observer. In JS it is called PubSub.
F. The Callback - The callback pattern is what enabled AJAX and it is revolutionizing development in terms of Window 8, Firefox OS, and Node.js - because of something called non-blocking-io. Very important.
Do not be afraid. This is the direction to go for long-term and advanced JavaScript implementations.
Once you recognize the patterns, it is down hill from there.
Hope this helps.