-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (32 loc) · 1.07 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
let People = function () { //第①步,创建People函数
function People(name, age) {//第②步,理解constructor就是指向People,People挂载着name和age两个属性
this.name = name;
this.age = age;
}
//将静态和动态的方法分别挂载在People的原型和People上。
creatClass(People, [{
key: "say", value: function () {
console.log(123)
}
}], [{
key: "see", value: function () {
alert("how are you")
}
}])
return People;
}
//这里的Constructor就是指的People
let creatClass = function ({
return function(Constructor, protoProps, staticProps){
//有原型上的方法挂载People.prototype上
if(protoProps){ defineProperties(Constructor.prototype, protoProps) }
//有People对象上的方法挂载People上
if (staticProps) { defineProperties(Constructor, staticProps) }}
//定义对象属性
let defineProperties = function (target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
Object.defineProperty(target, descriptor.key, descriptor);
}
}
})