-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path封装extends实现.js
47 lines (41 loc) · 934 Bytes
/
封装extends实现.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
38
39
40
41
42
43
44
45
46
47
/*
* @Author: yangyuan
* @Date: 2019-12-19 14:54:53
* @Email: [email protected]
* @LastEditTime : 2019-12-19 14:57:42
* @Description:
*/
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype = {
eat: function() {
console.log(this.name + '正在吃饭');
},
sang: function() {
console.log(this.name + '正在唱歌');
}
};
var liuyu = new Person('liuyu', 26);
function Student(name, age, score) {
Person.call(this, name, age);
this.score = score;
}
// 封装一个extends方法
Function.prototype.extends = function(func, options) {
for (const key in fun.prototype) {
this.prototype[key] = func.prototype[key];
}
for (const name in options) {
this.prototype[name] = options[name];
}
};
Student.extends(Person, {
study: function() {
console.log(this.name + '正学习...');
}
});
var can = new Student('can', 21, '良好');
can.eat();
can.study();