freeCodeCamp/guide/chinese/certifications/javascript-algorithms-and-d.../object-oriented-programming/use-an-iife-to-create-a-module/index.md

1.0 KiB
Raw Blame History

title localeTitle
Use an IIFE to Create a Module 使用IIFE创建模块

使用IIFE创建模块

方法

两个Mixin都必须包含在一个新的funModule所以一个funModule的起点是到目前为止注释掉所有的代码。

/*let isCuteMixin = function(obj) { 
  obj.isCute = function() { 
    return true; 
  }; 
 }; 
 let singMixin = function(obj) { 
  obj.sing = function() { 
    console.log("Singing to an awesome tune"); 
  }; 
 }; 
 */ 

然后在下面开始编写新的funModule代码。在新模块中您需要编写一个return语句来返回两个Mixin代码块。只需将原始的Mixin代码块复制到新的模块代码中但请记住将两个mixin与a分开,

let funModule = (function() { 
  return { 
    isCuteMixin: function(obj) { 
      obj.isCute = function() { 
        return true; 
      }; 
    }, 
    singMixin: function(obj) { 
      obj.sing = function() { 
         console.log("Singing to an awesome tune"); 
      }; 
    } 
  } 
 })();