javascript prototype inheritance example
function Animal (name, energy) {
this.name = name
this.energy = energy
}
Animal.prototype.eat = function (amount) {
console.log(`${this.name} is eating.`)
this.energy += amount
}
Animal.prototype.sleep = function (length) {
console.log(`${this.name} is sleeping.`)
this.energy += length
}
Animal.prototype.play = function (length) {
console.log(`${this.name} is playing.`)
this.energy -= length
}
function Dog (name, energy, breed) {
Animal.call(this, name, energy)
this.breed = breed
}
Dog.prototype = Object.create(Animal.prototype)
Dog.prototype.bark = function () {
console.log('Woof Woof!')
this.energy -= .1
}
const charlie = new Dog('Charlie', 10, 'Goldendoodle')
console.log(charlie.constructor)function Person(first, last, age, gender, interests) {
this.name = {
first,
last
};
this.age = age;
this.gender = gender;
this.interests = interests;
};
function Teacher(first, last, age, gender, interests, subject) {
Person.call(this, first, last, age, gender, interests);
this.subject = subject;
}
Also in JavaScript:
- react native toggle button with text
- Error: Cannot find module 'ejs'
- how to use if else statement in javascript
- js get current seconds
- simple express server
- javascript check if is array
- how remove child in jquery
- percentage formula in javascript
- javascript program german to english translation
- how to set background colour i js inline stylel
- jquery option selected
- ORDS GET json case sensitive data keys
- nodemon : The term 'nodemon' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1 + nodemon index
- javascript test if it is an array
- await on observable
- express case sensitive routing
- Mobx-react console warning related observer
- substring
- javascript ceiling
- javascript close window
- javascript indexof with condition
- get looping in jquery
- jquery multiple selectors
- how to get session value using javascript