-
Notifications
You must be signed in to change notification settings - Fork 0
/
proto1.html
84 lines (74 loc) · 1.9 KB
/
proto1.html
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
</head>
<body>
<script>
/* 拷贝继承
function Foo() {
this.name = "张三";
}
Foo.prototype.showName = function() {
console.log(this.name);
};
function Bar() {
Foo.call(this);
this.age = 21;
}
Bar.prototype.showAge = function() {
console.log(this.age);
};
function extend(subs, sups) {
for (var attr in sups.prototype) {
subs.prototype[attr] = sups.prototype[attr];
}
}
extend(Bar, Foo);
var F = new Foo();
var B = new Bar();
console.log(F, B); */
//类式继承
/* function Foo() {
this.name = "张三";
}
Foo.prototype.showName = function() {
console.log(this.name);
};
function Bar() {
Foo.call(this);
this.age = 21;
}
extend(Bar, Foo);
Bar.prototype.showAge = function() {
console.log(this.age);
};
function extend(subs, sups) {
var F = function() {};
F.prototype = sups.prototype;
subs.prototype = new F();
subs.prototype.constructor = subs;
}
var b = new Bar();
var f = new Foo();
b.showAge();
b.showName();
console.log(f, b); */
// 非构造函数继承
/* var foo = {
name: "张三"
};
var bar = extend(foo);
function extend(subs) {
var F = function() {};
F.prototype = subs;
return new F();
}
console.log(bar);
bar.name = "李四"; */
</script>
</body>
</html>