Object.create Multiple Inherit
by andypotanin
JavaScript
console.clear();
Object.allKeys = function( obj ) { var r = []; for( var i in obj ) { r.push( i ); }; return r; };
Object.create2 = function( proto ) {
var target = {};
for( var key in proto ) {
Object.defineProperty( target, key, Object.getOwnPropertyDescriptor( proto, key ) || { value: null } );
}
return target;
}
function Model( name ) {
this.name = name;
}
Object.defineProperties( Model, {
d1: { value: function m1() { console.log( 'd1', this ); }, enumerable: true },
d2: { value: function m2() { console.log( 'd2', this ); }, enumerable: false }
});
Object.defineProperties( Model.prototype, {
m1: { value: function m1() { console.log( 'm1', this ); }, enumerable: true },
m2: { value: function m2() { console.log( 'm2', this ); }, enumerable: false }
});
var Inherit0 = new Model( 'Override' );
var Inherit1 = Object.create( Inherit0 );
var Inherit2 = Object.create( Inherit1 );
var Inherit3 = Object.create( Inherit2 );
var Inherit4 = Object.create( Inherit3 );
var Inherit5 = Object.create( Model.prototype );
var Inherit7 = Object.create2( Inherit5.__proto__ );
Object.defineProperty( Model.prototype, 'm5', {
value: function m5() { console.log( 'm5', this ); }, enumerable: true, configurable: true
});
Object.defineProperty( Model.prototype, 'm5', { enumerable: true });
var Inherit8 = Object.create( Model );
Object.defineProperty( Model, 'd4', { value: function d4() {}, enumerable: true });
console.log( 'Inherit8', Inherit8 );
console.log( Inherit7.m3 );
Inherit1.m1();
Inherit2.m2();
Inherit3.m3();
Inherit4.m4();
Inherit5.m3();
// console.log( '5.m5', typeof Inherit5.m5 ); // function // Object.create() references the prototype which will update along w/ original
//console.log( Object.getOwnPropertyNames( Inherit0 ) ); // name, m4
//console.log( Object.getOwnPropertyNames( Inherit4 ) ); // none
//console.log( Object.keys( Inherit0 ) ); // name, m4
//console.log( Object.keys( Inherit4 ) ); //...