* fix: Chinese test suite Add localeTiltes, descriptions, and adjust test text and testStrings to get the automated test suite working. * fix: ran script, updated testStrings and solutions
2.7 KiB
2.7 KiB
id, title, challengeType, videoUrl, localeTitle
id | title | challengeType | videoUrl | localeTitle |
---|---|---|---|---|
587d7db2367417b2b2512b8c | Use an IIFE to Create a Module | 1 | 使用IIFE创建模块 |
Description
immediately invoked function expression
( IIFE
)通常用于将相关功能分组到单个对象或module
。例如,早期的挑战定义了两个mixins: function glideMixin(obj){我们可以将这些
obj.glide = function(){
console.log(“在水面上滑行”);
};
}
function flyMixin(obj){
obj.fly = function(){
console.log(“Flying,wooosh!”);
};
}
mixins
分组到一个模块中,如下所示: 让motionModule =(function(){请注意,您有一个
返回{
glideMixin:function(obj){
obj.glide = function(){
console.log(“在水面上滑行”);
};
},
flyMixin:function(obj){
obj.fly = function(){
console.log(“Flying,wooosh!”);
};
}
}
})(); //两个括号使得函数立即被调用
immediately invoked function expression
( IIFE
),它返回一个对象motionModule
。此返回对象包含作为对象属性的所有mixin
行为。 module
模式的优点是可以将所有运动行为打包到单个对象中,然后可以由代码的其他部分使用。以下是使用它的示例: motionModule.glideMixin(鸭);
duck.glide();
Instructions
funModule
的module
来包装两个mixins
isCuteMixin
和singMixin
。 funModule
应该返回一个对象。 Tests
tests:
- text: 应该定义<code>funModule</code>并返回一个对象。
testString: assert(typeof funModule === "object");
- text: <code>funModule.isCuteMixin</code>应该访问一个函数。
testString: assert(typeof funModule.isCuteMixin === "function");
- text: <code>funModule.singMixin</code>应该访问一个函数。
testString: assert(typeof funModule.singMixin === "function");
Challenge Seed
let isCuteMixin = function(obj) {
obj.isCute = function() {
return true;
};
};
let singMixin = function(obj) {
obj.sing = function() {
console.log("Singing to an awesome tune");
};
};
Solution
// solution required