-
Notifications
You must be signed in to change notification settings - Fork 0
/
javascript-extend-1.html
55 lines (37 loc) · 1.08 KB
/
javascript-extend-1.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>继承</title>
<script type="text/javascript" >
// 类式继承
function Person(name) {
this.name = name;
}
Person.prototype.getName = function() {
return this.name;
};
function Student(name,age) {
Person.call(this,name); // 将Person对象代替this
this.age = age;
}
console.log(new Person("张三"));
console.log(new Student("张四","25"));
Student.prototype = new Person();
console.log(new Student("张五","36")); // 这里的原型指向的是Person,注释以下部分可以看出
Student.prototype.constructor = Student; // 将Student 的原型指向自己
console.log(new Student("张六","40"));
Student.prototype.getAge = function() {
return this.age;
};
console.log(new Student("王五","26"));
// 实例化子类
var zhangsan = new Student("梅敏君","25");
console.log(zhangsan.getName()); //打印超类方法
console.log(zhangsan.getAge()); //打印自身方法
console.log(zhangsan); //打印对象
</script>
</head>
<body>
</body>
</html>