javascript inherit functions code example
Example 1: inherit javascript
function Animal() { }
Animal.prototype.eat = function() {
return "nom nom nom";
};
function Bird() { }
Bird.prototype = Object.create(Animal.prototype);
Bird.prototype.eat = function() {
return "peck peck peck";
};
Example 2: javascript inherit function
function inherit(c, p) {
Object.defineProperty(c, 'prototype', {
value: Object.assign(c.prototype, p.prototype),
writable: true,
enumerable: false
});
Object.defineProperty(c.prototype, 'constructor', {
value: c,
writable: true,
enumerable: false
});
}
function _inherit(c, ...p) {
p.forEach(item => {
Object.defineProperty(c, 'prototype', {
value: Object.assign(c.prototype, item.prototype),
writable: true,
enumerable: false
});
})
Object.defineProperty(c.prototype, 'constructor', {
value: c,
writable: true,
enumerable: false
});
}