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

发布于 2024-09-25 00:21:13 字数 368 浏览 7 评论 0原文

我想知道模块模式或构造函数/原型模式是否更适合我的工作。

基本上我使用的是不显眼的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

歌枕肩 2024-10-02 00:21:13

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

神也荒唐 2024-10-02 00:21:13

模块模式比原型模式更简单、更优雅。然而,首先考虑移动设备。它不是中/大型对象的相关模式,因为初始化需要在开始之前解析整个块。多个闭包还会创建垃圾收集器不会释放的循环依赖项(尤其是 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.

夏末 2024-10-02 00:21:13

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文