class inheritance method javascript
// Class Inheritance in JavaScript
class Mammal {
constructor(name) {
this.name = name;
}
eats() {
return `${this.name} eats Food`;
}
}
class Dog extends Mammal {
constructor(name, owner) {
super(name);
this.owner = owner;
}
eats() {
return `${this.name} eats Chicken`;
}
}
let myDog = new Dog("Spot", "John");
console.log(myDog.eats()); // Spot eats chickenfunction 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
});
}function Person(first, last, age, gender, interests) {
this.name = {
first,
last
};
this.age = age;
this.gender = gender;
this.interests = interests;
};
Also in JavaScript:
- json_encode escape \
- angular add bootstrap
- javascript pluck from array of objects
- base64 decode javascript
- backbone js event listener
- jquery add disabled to button
- Bitwise Operators js
- sum all elements in array javascript
- algolia react hits
- copyq script export tab to json
- js filter
- javascript .tolowercase
- nestjs version
- nodejs share session
- update one component from another component angular 9
- js get time in am
- js api call
- killall node windows
- variable key name js
- How to avoid scientific notation for large numbers in JavaScript?
- javascript array loop
- aos animation
- evento tecla enter javascript
- how to pass callback function in javascript