Javascript:模块模式与构造函数/原型模式? [英] Javascript: Module Pattern vs Constructor/Prototype pattern?
问题描述
我想知道模块模式或构造方法/ protoType模式是否更适用于我的工作。
I would like to know if the module pattern or Constructor/protoType pattern is more applicable to my work.
基本上我使用不引人注目的JavaScript - HTML文档引用了.js文件。
Basically I am using unobtrusive javascript -- the HTML document has a reference to the .js file.
我对模块模式的理解:
- 在INIT方法中,分配所有的点击事件等。
调用一个INIT方法(这个方法基本上是一个可以使用模块模式创建和返回的公共方法)
- 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:
- 用于创建对象
- 使用继承(即超类型的子类型)
我是正确的,为了提供不引人注目的javascript,模块模式是理想?
Am I correct, that for providing unobtrusive javascript, the module pattern is ideal?
推荐答案
Constr导出函数和原型是实现类和实例的合理方式之一。它们并不完全对应于该模型,因此您通常需要选择特定的方案或帮助方法来实现原型方面的类。 ( JS中的类的一些背景)
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.)
模块模式通常用于命名空间,其中您将有一个实例作为存储来分组相关的功能和对象。这是一个不同的用例,从什么样的原型是有益的。他们不是真的相互竞争;你可以很高兴地一起使用(例如,在模块中放置一个构造函数,并且说 new MyNamespace.MyModule.MyClass(arguments)
)。
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)
).
这篇关于Javascript:模块模式与构造函数/原型模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!