js原型继承

本文通过一个具体的JavaScript示例展示了如何实现原型链继承,包括父类和子类的定义及方法覆盖。

<!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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值