原型繼承
function superType() {
this.issuper = true;
}
superType.prototype.superFn = function () {
console.log('super function');
}
function subType() {
this.issub = true
}
subType.prototype = new superType();
subType.prototype.subFn = function () {
console.log('sub funciton');
}
借用構(gòu)造函數(shù)
function superType() {
this.issuper = true;
}
superType.prototype.superFn = function () {
console.log('super function');
}
function subType() {
superType.call(this);
this.issub = true
}
subType.prototype.subFn = function () {
console.log('sub funciton');
}
組合繼承
function superType() {
this.issuper = true;
}
superType.prototype.superFn = function () {
console.log('super function');
}
function subType() {
superType.call(this);
this.issub = true
}
subType.prototype = new superType();
subType.prototype.subFn = function () {
console.log('sub funciton');
}
原型式繼承
function object(o) {
function F() {}
F.prototype = o;
return new F();
}
ECMAScript5 中已經(jīng)規(guī)范這種繼承方式:
Object.create();
寄生式繼承
function createAnotherObj(original) {
var clone = object(original);
clone.sayHi = function () {
console.log('hi');
}
return clone;
}