继承
1.原型链继承
实例和原型之间构造一条原型链 (换句话就是通过原型搭建桥梁)
function SuperType(){
this.colors = ["red", "blue", "green"];
}
SuperType.prototype.Fun = function(){
};
function SubType(){
}
//继承了SuperType
SubType.prototype = new 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,black"
思路解析:就是实例指向SubType()函数,这个函数指向自己的原型是SuperType()函数,所以在添加元素时其实是添加在SuperType()中,因此每个实例都能共享这个属性
2.盗用构造函数
在子类构造函数中调用父类构造函数
function SuperType(){
this.colors = ["red", "blue", "green"];
}
function SubType(){
//继承了SuperType
SuperType.call(this);
}
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"
function SuperType(name){
this.name = name;
}
function SubType(){
//继承了SuperType,同时还传递了参数
SuperType.call(this, "Nicholas");
//实例属性
this.age = 29;
}
var instance = new SubType();
alert(instance.name); //"Nicholas";
alert(instance.age); //29
思路解析:就是subType()函数直接继承了superType()函数,只是继承而已,就是相当于复制了一份在自己身上,所以实例只能修改自身的属性值。
3.组合继承
使用原型链继承原型上的属性和方法,通过盗用构造函数继承实例属性
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;
}
SubType.prototype = new SuperType();//借用原型链继承方法,一次调用
SubType.prototype.constructor = SubType;
SubType.prototype.sayAge = function(){
alert(this.age);
};
var instance1 = new SubType("Nicholas", 29);
instance1.colors.push("black");
alert(instance1.colors); //"red,blue,green,black"
instance1.sayName(); //"Nicholas";
instance1.sayAge(); //29
var instance2 = new SubType("Greg", 27);
alert(instance2.colors); //"red,blue,green"
instance2.sayName(); //"Greg";
instance2.sayAge(); //27
思路解析:这个函数盗用构造函数继承属性,然后借用原型链继承方法
--------分割线
4.原型式继承
function object(o){
function F(){}
F.prototype = o;
return new F();
}
let person = {
name:'Nicholas',
friends:["Shelby","Court","Van"]
};
let another = object(person);
another.name = "Grey";
another.friends.push("Rob")
console.log(person.friends) //["Shelby","Court","Van","Rob"]
思路:就是object()对传进来的对象进行了一次浅复制
5.寄生式继承
这种方法和原型式继承比较接近
function createAnother(original){
var clone = object(original); //通过调用函数创建一个新对象
clone.sayHi = function(){ //以某种方式来增强这个对象
alert("hi");
};
return clone; //返回这个对象
}
var person = {
name: "Nicholas",
friends: ["Shelby", "Court", "Van"]
};
var anotherPerson = createAnother(person);
anotherPerson.sayHi(); //"hi"
思路:创建一个实现继承的函数,以某种方式增强对象,然后返回这个对象
6.寄生组合式继承
两个子类函数组合在一个父类构造函数中
function inheritPrototype(subType, superType){
var prototype = object(superType.prototype); //创建对象
prototype.constructor = subType; //增强对象
subType.prototype = prototype; //指定对象
}
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);
};
网友评论