继承主要就是通过prototype chaining 来实现的
1、Prototype Chaining:
function SuperType(){
this.property = true;
}
SuperType.prototype.getSuperValue = function(){
return this.property;
};
function SubType(){
this.subproperty = false;
}
SubType.prototype = new SuperType();
//new method
SubType.prototype.getSubValue = function (){
return this.subproperty;
};
//override existing method
SubType.prototype.getSuperValue = function (){
return false;
};
SubType.prototype.getSubValue = function (){
return this.subproperty;
};
var instance = new SubType();
alert(instance.getSuperValue()); //false
每一个构造函数都有一个prototype对象,实例和这个对象是有关联的,那么把这个对象指向别的对象实例,也就和那个对象的prototype产生了关联。
问题:
1、SuperType上的实例属性,到SubType实例上变成了prototype共享属性(原本不想公用的,又变成共用了)
2、没办法在不影响其他实例的前提下给SuperType传参数
2、Constructor Stealing:
function SuperType(name) {
this.colors = [“red”, “blue”, “green”];
this.name = name;
}
function SubType() {
SuperType.call(this, "chaugnwei"); 这样每个SubType的实例都会有自己的SuperType属性了,而且相对于上面方式,能给SuperType传参数
}
var instance1 = new SubType();
instance1.colors.push(“black”);
alert(instance1.colors); //”red,blue,green,black”
var instance2 = new SubType();
alert(instance2.colors); //”red,blue,green”
问题:
1、方法需要定义在构造函数里,没有重用
2、定义在SuperType的prototype里的东西是没有继承下来的(没有new)
3、Combination Inheritance(pseudoclassical inheritance),结合上面两种方式的优点:
function SuperType(name){
this.name = name;
this.colors = [“red”, “blue”, “green”];
}
SuperType.prototype.sayName = function(){
alert(this.name);
};
function SubType(name, age){
//inherit properties
SuperType.call(this, name);
this.age = age;
}
//inherit methods
SubType.prototype = new SuperType();
SubType.prototype.sayAge = function(){
alert(this.age);
};
var instance1 = new SubType(“Nicholas”, 29);
var instance2 = new SubType(“Greg”, 27);
问题:
supertype构造函数调用了两次
4、Prototypal Inheritance:
var person = {
name: “Nicholas”,
friends: [“Shelby”, “Court”, “Van”]
};
var anotherPerson = Object.create(person);
anotherPerson.name = “Greg”;
anotherPerson.friends.push(“Rob”);
var yetAnotherPerson = Object.create(person);
yetAnotherPerson.name = “Linda”;
yetAnotherPerson.friends.push(“Barbie”);
alert(person.friends); //”Shelby,Court,Van,Rob,Barbie”引用属性会共享
5、Parasitic Inheritance:
function createAnother(original){
var clone = Object.create(original); //create a new object by calling a function
clone.sayHi = function(){ //augment the object in some way
alert(“hi”);
};
return clone; //return the object
}
var person = {
name: “Nicholas”,
friends: [“Shelby”, “Court”, “Van”]
};
var anotherPerson = createAnother(person);
anotherPerson.sayHi(); //”hi”
可以看出,这种方式也没有公用函数的
6、Parasitic combination inheritance(结合Combination 和parasitic ),使用parasitic inheritance来继承超类的prototype:
function inheritPrototype(subType, superType){
var prototype = Object.create(superType.prototype); //create object
prototype.constructor = subType; //augment object
subType.prototype = prototype; //assign object
}
function SuperType(name){
this.name = name;
this.colors = [“red”, “blue”, “green”];
}
SuperType.prototype.sayName = function(){
alert(this.name);
};
function SubType(name, age){
SuperType.call(this, name);
this.age = age;
}
inheritPrototype(SubType, SuperType);
SubType.prototype.sayAge = function(){
alert(this.age);
};