显示模块模式略有变化 [英] Slight variation of the Revealing Module Pattern

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

问题描述

使用这个好的做法,而不是显示模块模式 .. 。

Is it good practice to use, instead of this Revealing Module Pattern ...

var MyModule = ( function() {
  function Method1() { alert( 'method1' ); }
  function Method2() { Method1(); alert( 'method2' ); }
  function Method3() { Method1(); alert( 'method3' ); }
  function Method4() { Method1(); alert( 'method4' ); }
  return { Method1 : Method1,    // these
           Method2 : Method2,    // lines
           Method3 : Method3,    // are 
           Method4 : Method4 };  // redundant...
} )();

MyModule.Method1(); 
MyModule.Method2();

...这种轻微变化:

... this slight variation:

var MyModule = {};
( function() {
  var Method1 = MyModule.Method1 = function () { alert( 'method1' ); };
  var Method2 = MyModule.Method2 = function () { Method1(); alert( 'method2' ); };
  var Method3 = MyModule.Method3 = function () { Method1(); alert( 'method3' ); };
  var Method4 = MyModule.Method4 = function () { Method1(); alert( 'method4' ); };
} )();

MyModule.Method1();
MyModule.Method2();

到底是100%相同吗?这被认为是好的做法吗?

推荐答案

这是不一样的。如果您要随时重命名您的模块,或希望以其他名称使用它,您将无法使用。

It's not the same. If you were to rename your module at any time or wish to use it in a different name, you won't be able to.

此外,通过将对象返回数组的结尾,你完全清楚了对象的内容。

Also, by returning the object at the end of the array, you make it perfectly clear what's exposed out of the object.

这篇关于显示模块模式略有变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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