js原型继承

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>

<script type="text/javascript">

Function.prototype.extend = function( fn){
for ( var attr in fn.prototype) {
this.prototype[attr] = fn.prototype[attr]
}
}
function Father(name,age,sex){
this.name = name;
this.age = age;
this.sex = sex;

}
Father.prototype.play = function(){
console.log(this.name+'打篮球')
}
Father.prototype.eat = function(){
console.log(this.name+'吃大米')
}
Father.prototype.drink = function(){
console.log(this.name+'喝牛奶')
}
function Child(name,age,sex,height){
Father.call( this,name,age,sex);
this.height = height;
}
Child.extend( Father )
Child.prototype.play = function(){
console.log(this.name+'dadada')
}
//
var father = new Father('张三','18','男');
var child = new Child('张四','18','男','160t')
child.play()
father.play()
alert(child.height)

</script>
</body>
</html>

原文地址:https://www.cnblogs.com/zzgyq/p/6529537.html