标签:
组合继承是js常用的继承模式,指的是将原型链和借用构造函数的技术结合在一起。其中的思想是使用原型链实现原型属性和方法的继承,
而通过借用构造函数实现对属性的继承。
例子:
<script>
function SuperType(name){
this.name = name;
this.colors = ["red","blue"];
}
SuperType.prototype.sayName = function(){
alert(this.name);
}
function SubType(name,age){
//继承属性
SuperType.call(this,name);
this.age = age;
}
//继承方法
SubType.prototype = new SuperType();
SubType.prototype.constructor = SubType;
//添加方法
SubType.prototype.sayAge = function(){
alert(this.age);
}
var instance1 = new SubType("jie",23);
instance1.colors.push("green");
alert(instance1.colors);
instance1.sayName();
instance1.sayAge();
var instance2 = new SubType("fei",23);
alert(instance2.colors);
instance2.sayName();
instance2.sayAge();
</script>
标签:
原文地址:http://www.cnblogs.com/scnuwangjie/p/4918608.html