javascript function inherite code example
Example 1: javascript inheritence
class child_class_name extends parent_class_name
Example 2: inherit javascript
function Animal() { }
Animal.prototype.eat = function() {
return "nom nom nom";
};
function Bird() { }
// Inherit all methods from Animal
Bird.prototype = Object.create(Animal.prototype);
// Bird.eat() overrides Animal.eat()
Bird.prototype.eat = function() {
return "peck peck peck";
};
Example 3: 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
});
}
// Or if you want multiple inheritance
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
});
}