Pseudo-classical inheritance (through prototyping): | function A () {
this.pro1 = 'pro1 ';
this.method1 = function(){ return this.pro1 }
}
function B() {
this.pro2 = 'pro2 ';
this.method2 = function(){return this.pro1 + this.pro2;};
}
B.prototype = new A();
function C() {
this.pro3 = 'pro3 ';
this.method3 = function(){return this.pro1 + this.pro2 + this.pro3 + this.method1() + this.method2();};
}
C.prototype = new B();
inst = new C()
inst.method3()
| ready |
Function application or "constructor chaining" | function A () {
this.pro1 = 'pro1 ';
this.method1 = function(){ return this.pro1 }
}
function B() {
this.pro2 = 'pro2 ';
this.method2 = function(){return this.pro1 + this.pro2;};
A.apply(this, arguments);
}
function C() {
this.pro3 = 'pro3 ';
this.method3 = function(){return this.pro1 + this.pro2 + this.pro3 + this.method1() + this.method2();};
B.apply(this, arguments);
}
inst = new C()
inst.method3()
| ready |
Copying prototype iterating the Function.prototype | function A(){}
A.prototype.pro1 = 'pro1 ';
A.prototype.method1 = function(){ return this.pro1 }
function B(){}
B.prototype.pro2 = 'pro2 ';
B.prototype.method2 = function(){return this.pro1 + this.pro2;};
for (i in A.prototype)
B.prototype[i] = A.prototype[i]
function C(){}
C.prototype.pro3 = 'pro3 ';
C.prototype.method3 = function(){return this.pro1 + this.pro2 + this.pro3 + this.method1() + this.method2();};
for (i in B.prototype)
C.prototype[i] = B.prototype[i]
inst = new C()
inst.method3()
| ready |
Parasitic inheritance or Power Constructors | function A() {
var A = {
pro1: 'pro1 ',
method1: function(){ return this.pro1 }
};
return A;
}
function B() {
var obj = A();
obj.pro2 = 'pro2 ';
obj.method2 = function(){return this.pro1 + this.pro2;};
return obj;
}
function C() {
var obj = B();
obj.pro3 = 'pro3 ';
obj.method3 = function(){return this.pro1 + this.pro2 + this.pro3 + this.method1() + this.method2();};
return obj;
}
inst = C();
inst.method3()
| ready |
ECMAScript 5th Ed. Object.create method: | var A = {
pro1: 'pro1 ',
method1: function(){ return this.pro1 }
};
var B = Object.create(A);
B.pro2 = 'pro2 ';
B.method2 = function(){return this.pro1 + this.pro2;};
var C = Object.create(B);
C.pro3 = 'pro3 ';
C.method3 = function(){return this.pro1 + this.pro2 + this.pro3 + this.method1() + this.method2();};
C.method3();
| ready |
Parasitic inheritance or Power Constructors (Iterating) | function A() {
var A = {
pro1: 'pro1 ',
method1: function(){ return this.pro1 }
};
return A;
}
function B() {
var obj = {};
obj.pro2 = 'pro2 ';
obj.method2 = function(){return this.pro1 + this.pro2;};
var a = A();
for (i in a)
obj[i] = a[i];
return obj;
}
function C() {
var obj = {};
obj.pro3 = 'pro3 ';
obj.method3 = function(){return this.pro1 + this.pro2 + this.pro3 + this.method1() + this.method2();};
var b = B();
for (i in b)
obj[i] = b[i];
return obj;
}
inst = C();
inst.method3()
| ready |
| function extend() {
var len = arguments.length;
var parent = (len > 1) ? arguments[0] : Function.constructor;
var body = arguments[len - 1];
body.prototype = parent.prototype;
var t = function (){
if (this.init)
this.init.apply(this, arguments);
};
t.prototype = new body(parent.prototype);
return t;
}
var A = extend(function (base) {
this.pro1 = 'pro1 ';
this.method1 = function() {
return this.pro1;
}
});
var B = extend(A, function(base) {
this.pro2 = 'pro2 ';
this.method2 = function() {
return this.pro1 + this.pro2;
};
});
var C = extend(B, function(base) {
this.pro3 = 'pro3 ';
this.method3 = function(){return this.pro1 + this.pro2 + this.pro3 + this.method1() + this.method2();};
});
ins=new C
ins.method3()
| ready |