Answers for "javascript inheritance"

8

prototype inheritance javascript

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)
Posted by: Guest on March-26-2020
6

javascript class inheritance

// 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 chicken
Posted by: Guest on May-25-2020
3

javascript inheritance

class Animal {
   null
}
class tiger extends Animal {
 null }
Posted by: Guest on May-15-2021
2

javascript inheritence

class child_class_name extends parent_class_name
Posted by: Guest on May-07-2021
1

javascript inheritance

class childClass extends parentClass
{
  
}
Posted by: Guest on September-02-2021
0

javascript inheritance

function Teacher(first, last, age, gender, interests, subject) {
  Person.call(this, first, last, age, gender, interests);

  this.subject = subject;
}
Posted by: Guest on February-17-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language