__proto__ 是指向的创建者的原型对象
constructor 是指向的创建者
prototype 是当前对象的原型对象
原型链继承
function Super(){
this.val = 1;
this.arr = [1];
}
function Sub(){
}
Sub.prototype = new Super();
原型式继承
function beget(obj){
var F = function(){};
F.prototype = obj;
return new F();
}
function Super(){
this.val = 1;
this.arr = [1];
}
var sup = new Super();
var sub = beget(sup);
构造函数继承
function Super(val){
this.val = val;
this.arr = [1];
this.fun = function(){
}
}
function Sub(val){
Super.call(this, val);
}
组合继承
function Super(){
this.val = 1;
this.arr = [1];
}
Super.prototype.fun1 = function(){};
Super.prototype.fun2 = function(){};
function Sub(){
Super.call(this);
}
Sub.prototype = new Super();
寄生式继承
function beget(obj){
var F = function(){};
F.prototype = obj;
return new F();
}
function Super(){
this.val = 1;
this.arr = [1];
}
function getSubObject(obj){
var clone = beget(obj);
clone.attr1 = 1;
clone.attr2 = 2;
return clone;
}
var sub = getSubObject(new Super());
寄生组合继承(推荐)
function beget(obj){
var F = function(){};
F.prototype = obj;
return new F();
}
function Super(){
this.val = 1;
this.arr = [1];
}
Super.prototype.fun1 = function(){};
Super.prototype.fun2 = function(){};
function Sub(){
Super.call(this);
}
var proto = beget(Super.prototype);
proto.constructor = Sub;
Sub.prototype = proto;
var sub = new Sub();