Files
Nicholas Carrigan (he/him) 3da4be21bb chore: seed chinese traditional (#42005)
Seeds the chinese traditional files manually so we can deploy to
staging.
2021-05-05 22:43:49 +05:30

2.3 KiB
Raw Permalink Blame History

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d7db2367417b2b2512b8c 使用 IIFE 創建一個模塊 1 301332 use-an-iife-to-create-a-module

--description--

一個立即調用函數表達式IIFE通常用於將相關功能分組到單個對象或者是 module 中。 例如,先前的挑戰中定義了兩個 mixins

function glideMixin(obj) {
  obj.glide = function() {
    console.log("Gliding on the water");
  };
}
function flyMixin(obj) {
  obj.fly = function() {
    console.log("Flying, wooosh!");
  };
}

我們可以將這些 mixins 分成以下模塊:

let motionModule = (function () {
  return {
    glideMixin: function(obj) {
      obj.glide = function() {
        console.log("Gliding on the water");
      };
    },
    flyMixin: function(obj) {
      obj.fly = function() {
        console.log("Flying, wooosh!");
      };
    }
  }
})();

注意一個立即調用函數表達式IIFE返回了一個 motionModule 對象。 返回的這個對象包含了作爲對象屬性的所有 mixin 行爲。 module 模式的優點是,所有的運動相關的行爲都可以打包成一個對象,然後由代碼的其他部分使用。 下面是一個使用它的例子:

motionModule.glideMixin(duck);
duck.glide();

--instructions--

創建一個名爲 funModule 的模塊,將這兩個 mixinsisCuteMixinsingMixin 包裝起來。 funModule 應該返回一個對象。

--hints--

funModule 應該被定義並返回一個對象。

assert(typeof funModule === 'object');

funModule.isCuteMixin 應該訪問一個函數。

assert(typeof funModule.isCuteMixin === 'function');

funModule.singMixin 應該訪問一個函數。

assert(typeof funModule.singMixin === 'function');

--seed--

--seed-contents--

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

--solutions--

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