Javascript:模块模式 vs 构造函数/原型模式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3790909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:10:50  来源:igfitidea点击:

Javascript: Module Pattern vs Constructor/Prototype pattern?

javascriptdesign-patternsunobtrusive-javascriptmodule-pattern

提问by Martin

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.

基本上我使用的是不显眼的 javascript——HTML 文档引用了 .js 文件。

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.
  • 调用 INIT 方法(这基本上是一个公共方法,我可以使用模块模式创建和返回)
  • 在 INIT 方法中,分配所有点击事件等。

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?

我是否正确,为了提供不显眼的 javascript,模块模式是理想的?

采纳答案by bobince

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.)

构造函数和原型是实现类和实例的合理方式之一。它们与该模型并不完全对应,因此您通常需要选择特定的方案或辅助方法来根据原型实现类。(有关 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)).

模块模式通常用于命名空间,您将有一个单独的实例作为存储来分组相关的函数和对象。这是一个不同于原型设计的用例。他们并不是真正地相互竞争;你可以很高兴地同时使用两者(例如,在一个模块中放入一个构造函数并 say new MyNamespace.MyModule.MyClass(arguments))。

回答by Yann VR

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

模块模式比原型更容易和优雅。但是,首先考虑移动。它不是中/大型对象的相关模式,因为初始化需要在开始之前解析整个块。多个闭包还会创建垃圾收集器不会释放的循环依赖项(尤其是 IE),这会导致在关闭窗口(或选项卡)之前不会释放更重的内存占用 - 检查 chrome 任务管理器进行比较 - 加载时间相反使用模块模式与对象大小成正比,而原型继承则不是这种情况。以上陈述通过多个基准进行验证,例如: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.

如上次测试所示。小对象最好初始化为普通对象(没有这些模式)。它适用于不需要闭包和继承的单个对象。明智的做法是评估您是否甚至需要这些模式。

回答by user1662008

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.

原型模式帮助我们扩展功能,并且无论对象数量如何,内存中都只有一个函数实例。在模块模式中,每个对象在内存中创建一个新的函数实例,但它提供了私有/公共变量的概念,并有助于封装变量和函数。