模块化
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// 完成函数 createModule, 调用之后满足如下要求:
// 1、 返回一个对象
// 2、 对象的 greeting 属性值等于 str1, name 属性值等于 str2
// 3、 对象存在一个 sayIt 方法, 该方法返回的字符串为 greeting属性值 + ', ' + name属性值
function createModule(str1, str2) {
function test(str1, str2) {
this.greeting = str1;
this.name = str2;
this.sayIt = function() {
return this.greeting + ',' + this.name;
}
}
return new test(str1, str2);
}
var res = createModule("hello", "ben");
console.log(res);
</script>
</body>
</html>
运行结果: