-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
93 lines (64 loc) · 2.15 KB
/
index.php
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
85
86
87
88
89
90
91
92
93
<script type="text/javascript">
//Here is prototype inheritance
//Man
function Man(age, name) {
this.age = age;
this.name = name;
}
Man.prototype.live = function() {
return this.age > 0;
};
//Student
function Student(age, name) {
Student.superclass.apply(this, arguments);
this.study = function() {
return this.age--;
};
}
Student.superclass = Man;
Student.prototype.constructor = Student;
Student.prototype.assign = Man.prototype;
console.log("{Man.prototype} = ", Man.prototype);
console.log("{Student.prototype} = ", Student.prototype);
console.log("{new Man(1, 2)} = ", new Man(1, 2));
console.log("{new Student(1, 2)} = ", new Student(1, 2));
</script>
<script type="text/javascript">
//Here is Object.create inheritance
//Man
function Man2(age, name) {
this.age = age;
this.name = name;
}
Man2.prototype.live = function() {
return this.age > 0;
};
//Student
function Student2(age, name) {
Man2.call(this, age, name);
this.study = function() {
return this.age--;
};
}
Student2.prototype = Object.create(Man2.prototype);
Student2.prototype.constructor = Student2;
console.log("{Man2.prototype} = ", Man2.prototype);
console.log("{Student2.prototype} = ", Student2.prototype);
console.log("{new Man2(1, 2)} = ", new Man2(1, 2));
console.log("{new Student2(1, 2)} = ", new Student2(1, 2));
</script>
<script type="text/javascript">
//Duck Type
function duckType(obj) {
return (obj.hasOwnProperty('study'))?'student':'man';
}
console.log("{ duckType( new Man2()) } = ", duckType( new Man2() ) );
console.log("{ duckType( new Student()) } = ", duckType( new Student() ) );
</script>
<script type="text/javascript">
function duckType2() {
return (this.hasOwnProperty('study'))?'student':'man';
}
console.log("{duckType2.call( new Man() )} = ", duckType2.call( new Man() ) );
console.log("{duckType2.call( new Student2() )} = ", duckType2.call( new Student2() ) );
</script>