Javascript模块模式有什么好处? [英] What is the benefit of the Javascript Module Pattern?

查看:71
本文介绍了Javascript模块模式有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究为我的团队提出标准化的Javascript编码风格。大多数资源现在推荐涉及闭包的模块模式,例如:

I have been doing research to come up with a standardized Javascript coding style for my team. Most resources now recommend the "Module" pattern that involves closures, such as this:

var Module = function() { 

    someMethod = function() { /* ... */ };

    return { 
        someMethod: someMethod
    };

}();

并像 Module.someMethod(); 。这种方法似乎只适用于在传统OOP上下文中是静态的方法,例如用于获取/保存数据的存储库类,用于发出外部请求的服务层等。除非我遗漏了某些内容,否则模块模式不适用于通常需要从服务方法传递到UI胶水代码的数据类(想想DTO)。

and invoke it like Module.someMethod();. This approach seems to only work with methods that would be static in a traditional OOP context, for example repository classes to fetch/save data, service layers to make outside requests, and the like. Unless I missed something, the module pattern isn't intended to be used with data classes (think DTOs) that would typically need to be passed to/from the service methods to the UI glue code.

我看到的一个常见好处是,您可以在Javascript中使用模块模式获得真正的私有方法和字段,但这也可以实现以及能够具有静态实例方法,其经典Javascript样式与此类似:

A common benefit I see cited is that you can have true private methods and fields in Javascript with the module pattern, but this can also be achieved along with being able to have static or instance methods with the "classical" Javascript style similar to this:

myClass = function(param) { 
    // this is completely public
    this.publicProperty = 'Foo';

    // this is completely private
    var privateProp = param;

    // this function can access the private fields
    // AND can be called publicly; best of both?
    this.someMethod = function() { 
        return privateProp;
    };

    // this function is private.  FOR INTERNAL USE ONLY
    function privateMethod() { 
        /* ... */
    };
}

// this method is static and doesn't require an instance
myClass.staticMethod = function() { /* ... */ };

// this method requires an instance and is the "public API"
myClass.prototype.instanceMethod = function() { /* ... */ };

所以我想我的问题是什么使模块模式比传统风格更好?它有点清洁,但这似乎是唯一明显的好处;事实上,传统风格似乎提供了提供真正封装的能力(类似于真正的OOP语言,如Java或C#),而不是简单地返回一组静态方法。

So I guess my question is what makes the Module Pattern better than the traditional style? It's a bit cleaner, but that seems to be the only benefit that is immediately apparent; in fact, the traditional style seems to offer the ability to provide real encapsulation (similar to true OOP languages like Java or C#) instead of simply returning a collection of static-only methods.

我有什么遗漏吗?

推荐答案

上面的模块模式毫无意义。您所做的就是使用一个闭包来返回带有原型的构造函数。您可以通过以下方式实现相同目标:

The module pattern above is pointless. All you are doing is using one closure to return a constructor with prototype. You could have achieved the same with:

function Module() {};
Module.prototype.whatever = function() {};

var m = new Module();
m.whatever();

实际上,您可以使用相同的输出保存一个对象(闭包)。

In fact you would have saved one object (the closure) from being created, with the same output.

我对模块模式的另一个好处是,如果你将它用于私有封装,你只能轻松地使用单例而不是具体的类。要创建具有私有数据的具体类,最终会包含两个闭包器,这会变得很难看。我也同意,当它们可见时,能够调试下划线伪私有属性要容易得多。 如果某人错误地使用你的课程会怎么样的整个概念从来都不合理。制作一个干净的公共API,记录它,如果人们没有正确地遵循它,那么你的团队中就有一个糟糕的程序员。 Javascript隐藏变量(可以在Firefox中使用eval发现)所需的工作量不值得JS的典型用法,即使对于大中型项目也是如此。人们不会窥探你的物体来学习它们,他们会阅读你的文档。如果你的文档很好(例如使用JSDoc)那么他们就会坚持下去,就像我们使用我们需要的每个第三方库一样。我们不会篡改jQuery或YUI,我们只是信任并使用公共API,而不会过多关注它下面使用的方式或内容。

My other beef with the module pattern is it if you are using it for private encapsulation, you can only use it easily with singletons and not concrete classes. To create concrete classes with private data you end up wrapping two closers, which gets ugly. I also agree that being able to debug the underscore pseudo-private properties is a lot easier when they are visible. The whole notion of "what if someone uses your class incorrectly" is never justified. Make a clean public API, document it, and if people don't follow it correctly, then you have a poor programmer in your team. The amount of effort required in Javascript to hide variables (which can be discovered with eval in Firefox) is not worth the typical use of JS, even for medium to large projects. People don't snoop around your objects to learn them, they read your docs. If your docs are good (example using JSDoc) then they will stick to it, just like we use every 3rd party library we need. We don't "tamper" with jQuery or YUI, we just trust and use the public API without caring too much how or what it uses underneath.

这篇关于Javascript模块模式有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆