[Object]继承(经典版)(四)多重继承和组合继承

作者:zccst


[b]一、多重继承[/b]
不得不说,这又是异常精彩的章节,是原型链的升华。

//父类1
function Parent1(name,age){
this.name = name;
this.age = age;
this.height=180;
}
Parent1.prototype.say = function(){
alert('hi...');
}

//父类2
function Parent2(name,age,weight){
this.name = name;
this.age = age;
this.weight = weight;
this.height = 170;
this.skin='yellow';
}
Parent2.prototype.walk = function(){
alert('walk...');
}

//子类
function Child(name,age,weight){
Parent1.call(this,name,age);
Parent2.call(this,name,age,weight);
}

//建立联系
for(var i in Parent1.prototype){Child.prototype[i] = Parent1.prototype[i]}
for(var i in Parent2.prototype){Child.prototype[i] = Parent2.prototype[i]}

var c1 = new Child('xiaoming',10,8);
console.log(c1);



其实,多重继承才是真实的物理世界,有一位博友用多重继承实现了照片墙,甚是精彩。
可以说是受教了,开眼界了。

多重继承还是很强大的。
博主的主要思想是循环复制多个父类原型对象的属性和方法到子类的原型对象,这样子类就可以继承多个父类了。

照片墙是个很有说服力的例子。其关系是:

Photo //会选择,鼠标移上,鼠标离开
Child1 //继承Photo,重写鼠标移上,离开。目的是移上放大三倍,离开回到原来尺寸。
Child2 //继承Photo,实现旋转10-40度
Child3 //继承Child1和Child2,实现即放大三倍,有选择10-40度


[b]二、组合继承[/b]

类似这样
function C(){
B.call(this, xx);
A.call(this, xx);
}


如果您觉得本文的内容对您的学习有所帮助,您可以微信:
[img]http://dl2.iteye.com/upload/attachment/0109/0668/fb266dfa-95ca-3d09-b41e-5f04a19ba9a1.png[/img]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值