0

使用它是一种好习惯,而不是这种显示模块模式......

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();

...这个细微的变化:

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%一样吗?这会被认为是好的做法吗?

4

3 回答 3

1

最后是100%一样吗?

最终结果是一样的。

这会被认为是好的做法吗?

这主要是见仁见智。您的示例是 RMP 的几个变体中的两个,都是有效的。

常见的第三种变体与您的第二种相似,但具有特定目的:

var MyModule = function(mod) {
  var Method1 = mod.Method1 = function () { alert( 'method1' ); };
  var Method2 = mod.Method2 = function () { Method1(); alert( 'method2' ); };
  var Method3 = mod.Method3 = function () { Method1(); alert( 'method3' ); };
  var Method4 = mod.Method4 = function () { Method1(); alert( 'method4' ); };
  return mod;
}(typeof MyModule === "undefined" ? {} : MyModule);

目的是允许添加到已定义的模块,但如果尚未定义则创建它。

于 2014-12-10T09:15:53.837 回答
1

这是不一样的。如果您要在任何时候重命名您的模块或希望以不同的名称使用它,您将无法这样做。

此外,通过在数组末尾返回对象,您可以非常清楚地从对象中暴露出什么。

于 2014-12-10T09:16:14.977 回答
1

您的第一个和第二个变体不一样。第一个是我称之为“自给自足”的 IIFE——IIFE 的主体在不假设任何关于 IIFE 之外的世界的信息的情况下完成其工作。您的第二个变体假设MyModule外部世界中有一个对象。虽然这是一个小假设,但我更喜欢以独立的方式编写我所有的 IIFE。

第二个变体可以通过稍作修改而自成一体:

var MyModule = {};
( function(MyModule) {
  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);

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

通过将创建的外部对象作为参数传递给 IIF,IIFE 中的代码不再对外部世界做出假设,并且可以更轻松地移动到代码库中的不同位置。

然而,你也问

这会被认为是好的做法吗?

我对此的回答是,你根本不应该使用显示模块模式!Revealing Module Pattern 有一个重要的缺陷,让我认为它是一种反模式,所以我建议完全不要使用它。坚持原来的模块模式,而不是显示模块。

于 2014-12-11T05:02:33.400 回答