作者:zccst
[b]一、多重继承[/b]
不得不说,这又是异常精彩的章节,是原型链的升华。
其实,多重继承才是真实的物理世界,有一位博友用多重继承实现了照片墙,甚是精彩。
可以说是受教了,开眼界了。
多重继承还是很强大的。
博主的主要思想是循环复制多个父类原型对象的属性和方法到子类的原型对象,这样子类就可以继承多个父类了。
照片墙是个很有说服力的例子。其关系是:
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]
[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]