继承

原型链

所有的函数默认的原型都是Object的实例,所有的引用类型都默认继承了Object,以原型链实现。
缺陷:父类属性共享问题

console.log("原型链");
function SuperType(){
	this.colors = ["red", "blue", "green"];
	this.property = true;
}
SuperType.prototype.getSuperValue = function() {
	// body...
	return this.property;
};
function SubType(){
	this.subproperty = false;
}
//继承了SuperType
SubType.prototype = new SuperType();
SubType.prototype.getSubValue = function() {
	// body...
	return this.subproperty;
};
var instance1 = new SubType();
console.log(instance1.getSuperValue());	//true
instance1.colors.push("black");
console.log(instance1.colors);	//[ 'red', 'blue', 'green', 'black' ]
var instance2 = new SubType();
console.log(instance2.colors);	//[ 'red', 'blue', 'green', 'black' ]

借用构造函数

缺陷:函数无法复用
超类中原型定义的函数子类无法复用,因为只是借用了一下构造函数的执行过程

console.log("借用构造函数");
function SuperType(){
	this.colors = ["red", "blue", "green"];
}
function SubType(){
	//继承了SuperType
	SuperType.call(this);
}
var instance1 = new SubType();
instance1.colors.push("black");
console.log(instance1.colors);	//[ 'red', 'blue', 'green', 'black' ]
var instance2 = new SubType();
console.log(instance2.colors);	//[ 'red', 'blue', 'green' ]

伪经典继承

最常用的继承模式

console.log("组合继承/伪经典继承");
function SuperType(name){
	this.name = name;
	this.colors = ["red", "blue", "green"];
}
SuperType.prototype.sayName = function() {
	// body...
	console.log(this.name);
};
function SubType(name, age){
	SuperType.call(this, name);
	this.age = age;
}
SubType.prototype = new SuperType();
SubType.prototype.sayAge = function() {
	// body...
	console.log(this.age);
};
var instance1 = new SubType("Nicholas", 29);
instance1.colors.push("black");
console.log(instance1.colors);	//[ 'red', 'blue', 'green', 'black' ]
instance1.sayName();			//Nicholas
instance1.sayAge();				//29
var instance2 = new SubType("Greg", 27);	
console.log(instance2.colors);	//[ 'red', 'blue', 'green' ]
instance2.sayName();			//Greg
instance2.sayAge();				//27

原型式继承

原型的值是共享的

console.log("原型式继承");
//以下函数类似于Object.create
function object(o){
	function F(){}
	F.prototype = o;
	return new F();
}
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");
console.log(person.friends);

寄生式继承

函数不复用

function object(o){
	function F(){}
	F.prototype = o;
	return new F();
}
function createAnother(original){
	var clone = object(original);
	clone.sayHi = function(){
		console.log("hi");
	}
	return clone;
}
var person = {
	name:"Nicholas",
	friends:["Shelby", "Court", "Van"]
}
var anotherPerson = createAnother(person);
anotherPerson.sayHi();	//hi

寄生组合式继承

console.log("寄生组合式继承");
function SuperType(name){
	this.name = name;
	this.colors = ["red", "blue", "green"];
}
SuperType.prototype.sayName = function(){
	console.log(this.name);
};
function SubType(name, age){
	SuperType.call(this, name);			//第二次调用SuperType()
	this.age = age;
}
SubType.prototype = new SuperType();	//第一次调用SuperType()
SubType.prototype.constructor = SubType;
SubType.prototype.sayAge = function(){
	console.log(this.age);
};

基本模式如下:

function inheritPrototype(subType, superType){
    var prototype = object(superType.prototype);    //创建对象
    prototype.constructor = subType;                //增强对象
    subType.prototype = prototype;                  //指定对象
}
posted @ 2016-05-19 14:17  西鬼  阅读(292)  评论(0编辑  收藏  举报