继承
创建对象
组合使用构造函数模式和原型模式
function Person(name = 'zhenjianyu',age = 26,job = '前端开发'){
this.name = name
this.age = age
this.job = job
}
Person.prototype.work = function(){
console.log('coding')
}
构造函数、原型和实例的关系:每个构造函数都有一个原型对象 prototype ,原型对象都包含一个指向构造函数的指针 constructor ,而实例都包含一个指向原型对象的内部指针 __proto__ (读作:双下划线proto双下划线)。
原型链继承
function SuperType(){
this.property = true
}
SuperType.prototype.getSuperValue = function(){
return this.property
}
function SubType(){
this.subproperty = false
}
SubType.prototype = new SuperType()
SubType.prototype.getSubValue = function(){
return this.subproperty
}
借用构造函数(伪造对象或经典继承)
function SuperType(){
this.colors = ['red','blue','green']
}
function SubType(){
SuperType.call(this)
}
组合继承(伪经典继承)
function Parent(name){
this.name = name;
this.colors = ['red', 'blue', 'green'];
}
Parent.prototype.getName = function(){
console.log(this.name);
}
function Child(name,age){
Parent.call(this,name);// 第二次调用 Parent()
this.age = age;
}
Child.prototype = new Parent(); // 第一次调用 Parent()
Child.prototype.constructor = Child;
原型式继承
function SuperType(name){
this.name = name
}
function content(obj){
function F(){}
F.prototype = obj
return new F
}
let superType = new SuperType()
let sub = content(superType)
寄生式继承
function SuperType(name){
this.name = name
}
function content(obj){
function F(){}
F.prototype = obj
return new F
}
let superType = new SuperType()
function subObject(obj){
let sub = content(obj)
sub.age = 18
return sub
}
let sub = subObject(superType)
寄生组合式继承
function SuperType(name){
this.name = name
}
function content(obj){
function F(){}
F.prototype = obj
return new F
}
let con = content(SuperType.prototype)
function SubType(){
SuperType.call(this)
}
SubType.prototype = con
con.constructor = SubType
let sub = new SubType()
es6继承
待补充
未完待续...
以自己现在的努力程度,还没有资格和别人拼天赋

浙公网安备 33010602011771号