Prototypal inheritance

by dsbonev

JavaScript

function Parent(name) {
    this.name = name;    
}
    
Parent.prototype.parentPrototypeMember = true ;
    
var child = Object.create(new Parent('John'));
debugInfo('Inherits from instance:', child);

child = Object.create(Parent.prototype);
debugInfo('Inherits from prototype:', child);

child = Object.create(Parent.prototype, {
    name: {value: 'Jack'}
});
debugInfo('Inherits from prototype and defines own members:', child);

function debugInfo(title, child) {
    console.log(title);
    console.log('\tinherits prototype members: ' + !!child.parentPrototypeMember);
    console.log('\thas name: ' + !!child.name);
}