-
Notifications
You must be signed in to change notification settings - Fork 1
/
es5继承.html
36 lines (32 loc) · 1.13 KB
/
es5继承.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
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
</body>
<script type="text/javascript">
"use strict"
function Fn(name,age){
this.name=name; //构造函数的属性多样
this.age=age;
if((typeof Fn.prototype.eat)!="funciton"){ //判断语句中是否有该方法,没有则创建
Fn.prototype.eat=function(){ //原型的方法共享
console.log(this.name+"吃了饭");
}
}
}
function Son(name,age,sex){ //创建子类构造函数
Fn.call(this,name,age) //借调Fn()的属性
this.sex=sex;
};
Son.prototype=new Fn(); //Son.prototype指向父类对象,实现了继承,所以能够调用eat方法,
var s1 = new Son("李四",20,"男"); //若没有继承,单单的使用call借调Fn继承,子类实例s1无法调用eat方法
console.log(s1); //因为call不是真正的继承
s1.eat();
var a = {a:3,c:3}
console.log(JSON.stringify(a,function(key,value){
return value
},2))
</script>
</html>