1、继承
(1)传统形式:原型链
过多的继承了没用的属性
(2)借用构造函数
不能继承借用构造函数的原型
每次构造函数都要多走一个函数
(3)共享原型
不能随便改动自己的原型
(4)圣杯模式
2、继承实例
(1)原型链继承
Grand.prototype.lastName = 'aa'
function Grand ( ){ }
var grand = new Grand( )
Father.prototype = grand
function Father ( ){
this.name = 'bb'
}
var father = new Father( )
Son.prototype = father
function Son ( ){ }
var son = new Son( )
(2)借用构造函数继承
function Person(name, age, sex){
this.name = name;
this.age = age;
this.sex = sex;
}
function Student(name, age, sex, grade){
Person.call(this, name, age, sex)
this.grade = grade
}
var student = new Student('aa', 18, 'man', 'one' )
(3)组合继承apply+prototype
(4)寄生继承
function Cat(o){
var obj = Object.create(o)
o.type = "动物"
return obj;
}
var obj = {name:"tom"}
var c = Cat(obj)
(5)共享原型继承
Father.prototype.lastName = 'aaa'
function Father( ){ }
Son.prototype = Father.prototype
function Son( ){ }
var son = new Son( )
(6)圣杯模式
function inherit (target, origin){
function F ( ){ }
F.prototype = Origin.prototype;
Target.prototype = new F( )
Target.prototype.constructor = Target
Target.prototype.uber = Origin.prototype
}
Father.prototype.lastName = 'aaa'
function Father ( ){ }
function Son ( ){ }
inherit(Son, Father)
var son = new Son( )
var father = new Father( )