Javascript:模块模式与构造函数/原型模式?
我想知道模块模式或构造函数/原型模式是否更适合我的工作。
基本上我使用的是不显眼的 javascript —— HTML 文档有对 .js 文件的引用。
我对模块模式的理解:
- 调用 INIT 方法(这基本上是我可以使用模块模式创建和返回的公共方法)
- 在 INIT 方法中,分配所有单击事件等。
这听起来像是适合我的情况的完美模式,因为我不需要创建对象和继承层次结构等。
我对构造函数/原型模式的理解:
- 对象
- 用于创建使用继承的
(即超类型的子类型)我是否正确,为了提供不引人注目的 JavaScript,模块模式是理想的?
I would like to know if the module pattern or Constructor/protoType pattern is more applicable to my work.
Basically I am using unobtrusive javascript -- the HTML document has a reference to the .js file.
My understanding of the module pattern:
- call an INIT method (which is basically a public method i can create and return using the module pattern)
- In the INIT method, assign all click events etc.
This sounds like the perfect pattern for my situation, as I don't need to create Objects and inheritance hierarchies etc.
My understanding of the Constructor/Prototype pattern:
- for creating objects
- for using inheritance (i.e. Subtypes of a supertype)
Am I correct, that for providing unobtrusive javascript, the module pattern is ideal?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
构造函数和原型是实现类和实例的合理方法之一。它们并不完全对应于该模型,因此您通常需要选择特定的方案或辅助方法来根据原型实现类。 。)
(JS 中的类的一些背景 模块模式通常用于命名空间,其中您将有一个实例充当存储来对相关函数和对象进行分组。这与原型设计的用途不同。他们并不是真正在相互竞争;而是在相互竞争。您可以非常愉快地一起使用两者(例如,将构造函数放入模块中并说
new MyNamespace.MyModule.MyClass(arguments)
)。Constructor-functions and prototypes are one of the reasonable ways to implement classes and instances. They don't quite correspond to that model so you typically need to choose a particular scheme or helper method to implement classes in terms of prototypes. (Some background on classes in JS.)
The module pattern is typically used for namespacing, where you'll have a single instance acting as a store to group related functions and objects. This is a different use case from what prototyping is good for. They're not really competing with each other; you can quite happily use both together (eg put a constructor-function inside a module and say
new MyNamespace.MyModule.MyClass(arguments)
).模块模式比原型模式更简单、更优雅。然而,首先考虑移动设备。它不是中/大型对象的相关模式,因为初始化需要在开始之前解析整个块。多个闭包还会创建垃圾收集器不会释放的循环依赖项(尤其是 IE),它会导致较重的内存占用,直到窗口(或选项卡)关闭后才会释放 - 检查 chrome 任务管理器进行比较 -
使用模块模式,加载时间与对象大小成反比,而原型继承则不是这样。
上述陈述通过多个基准测试进行了验证,例如:http://jsperf.com/prototypal-performance/54< /a>
如上次测试所示。小对象最好初始化为普通对象(没有这些模式)。它适用于不需要闭包或继承的单个对象。明智的做法是评估您是否需要这些模式。
Module pattern is by far easier and more elegant than prototype. However, thinking mobile first. It is not a relevant pattern for medium/large objects because the initialization needs to parse the whole block before starting. The multiple closures also create circular dependencies that the garbage collector does not free (especially IE), it results in a heavier memory footprint not freed until the window (or tab) is closed - check chrome task manager to compare-
The loading time is inversely proportional to the object size using module pattern while this is not the case for prototypal inheritance.
Statements above are verified through multiple benchmarks like this one: http://jsperf.com/prototypal-performance/54
As seen in last test. Small objects are better off being initialized as plain object ( without these patterns). It is suitable for single objects not requiring closure nor inheritance. It is wise to assess if you even need these patterns.
原型模式帮助我们扩展功能,并且无论对象数量有多少,内存中都只有一个函数实例。在模块模式中,每个对象在内存中创建一个新的函数实例,但它提供了私有/公共变量的概念,并有助于封装变量和函数。
Prototype pattern helps us to extend the functionality and there is only one instance of functions in a memory irrespective of the number of objects. In Module patter, each object creates a new instance of functions in memory but it provides with concept of private/public variables and helps in encapsulating the variables and functions.