Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Javascript Encapsulation Inheritance Polymorphism Composition

class Animal {
  eat() {
    return "Rax eat";
  }
}

class Dog extends Animal {
  sound() {
    return "Dog barks";
  }
}

class Cat extends Animal {
  sound() {
    return "Cat meows";
  }
}

class Home {
  constructor() {
    this.pets = []; // Could be a `new Set` for better efficiency
  }
  adoptPet(animal) {
    if (this.pets.includes(animal)) {
      throw new Error("Cannot adopt the same pet twice");
    }
    this.pets.push(animal);
  }
  makeAllSounds() {
    for (let pet of this.pets) {
      console.log(pet.sound());
    }
  }
}

var home = new Home();
var dog1 = new Dog();
var dog2 = new Dog();
var cat = new Cat();

home.makeAllSounds(); // this doesn't give/return any result/data
console.log("== Adding dog ==");
home.adoptPet(dog1);
home.makeAllSounds(); // this prints: Dog barks
console.log("== Adding cat ==");
home.adoptPet(cat);
home.makeAllSounds(); // this prints : Dog barks / Cat meows
console.log("== Adding dog ==");
home.adoptPet(dog2);
home.makeAllSounds(); // ...
home.adoptPet(dog1) // not ok!
 Run code snippet
Comment

Javascript Encapsulation Inheritance Polymorphism Composition

class Animal {
  eat() {
    return "Rax eat";
  }
}

class Dog extends Animal {
  sound() {
    return "Dog barks";
  }
}

class Cat extends Animal {
  sound() {
    return "Cat meows";
  }
}

class Home {
  constructor() {
    this.pets = []; // Could be a `new Set` for better efficiency
  }
  adoptPet(animal) {
    if (this.pets.includes(animal)) {
      throw new Error("Cannot adopt the same pet twice");
    }
    this.pets.push(animal);
  }
  makeAllSounds() {
    for (let pet of this.pets) {
      console.log(pet.sound());
    }
  }
}

var home = new Home();
var dog1 = new Dog();
var dog2 = new Dog();
var cat = new Cat();

home.makeAllSounds(); // this doesn't give/return any result/data
console.log("== Adding dog ==");
home.adoptPet(dog1);
home.makeAllSounds(); // this prints: Dog barks
console.log("== Adding cat ==");
home.adoptPet(cat);
home.makeAllSounds(); // this prints : Dog barks / Cat meows
console.log("== Adding dog ==");
home.adoptPet(dog2);
home.makeAllSounds(); // ...
home.adoptPet(dog1) // not ok!
 Run code snippet
Comment

Javascript Encapsulation Inheritance Polymorphism Composition

class Animal {
  eat() {
    return "Rax eat";
  }
}

class Dog extends Animal {
  sound() {
    return "Dog barks";
  }
}

class Cat extends Animal {
  sound() {
    return "Cat meows";
  }
}

class Home {
  constructor() {
    this.pets = []; // Could be a `new Set` for better efficiency
  }
  adoptPet(animal) {
    if (this.pets.includes(animal)) {
      throw new Error("Cannot adopt the same pet twice");
    }
    this.pets.push(animal);
  }
  makeAllSounds() {
    for (let pet of this.pets) {
      console.log(pet.sound());
    }
  }
}

var home = new Home();
var dog1 = new Dog();
var dog2 = new Dog();
var cat = new Cat();

home.makeAllSounds(); // this doesn't give/return any result/data
console.log("== Adding dog ==");
home.adoptPet(dog1);
home.makeAllSounds(); // this prints: Dog barks
console.log("== Adding cat ==");
home.adoptPet(cat);
home.makeAllSounds(); // this prints : Dog barks / Cat meows
console.log("== Adding dog ==");
home.adoptPet(dog2);
home.makeAllSounds(); // ...
home.adoptPet(dog1) // not ok!
 Run code snippet
Comment

Javascript Encapsulation Inheritance Polymorphism Composition

class Animal {
  eat() {
    return "Rax eat";
  }
}

class Dog extends Animal {
  sound() {
    return "Dog barks";
  }
}

class Cat extends Animal {
  sound() {
    return "Cat meows";
  }
}

class Home {
  constructor() {
    this.pets = []; // Could be a `new Set` for better efficiency
  }
  adoptPet(animal) {
    if (this.pets.includes(animal)) {
      throw new Error("Cannot adopt the same pet twice");
    }
    this.pets.push(animal);
  }
  makeAllSounds() {
    for (let pet of this.pets) {
      console.log(pet.sound());
    }
  }
}

var home = new Home();
var dog1 = new Dog();
var dog2 = new Dog();
var cat = new Cat();

home.makeAllSounds(); // this doesn't give/return any result/data
console.log("== Adding dog ==");
home.adoptPet(dog1);
home.makeAllSounds(); // this prints: Dog barks
console.log("== Adding cat ==");
home.adoptPet(cat);
home.makeAllSounds(); // this prints : Dog barks / Cat meows
console.log("== Adding dog ==");
home.adoptPet(dog2);
home.makeAllSounds(); // ...
home.adoptPet(dog1) // not ok!
 Run code snippet
Comment

Javascript Encapsulation Inheritance Polymorphism Composition

class Animal {
  eat() {
    return "Rax eat";
  }
}

class Dog extends Animal {
  sound() {
    return "Dog barks";
  }
}

class Cat extends Animal {
  sound() {
    return "Cat meows";
  }
}

class Home {
  constructor() {
    this.pets = []; // Could be a `new Set` for better efficiency
  }
  adoptPet(animal) {
    if (this.pets.includes(animal)) {
      throw new Error("Cannot adopt the same pet twice");
    }
    this.pets.push(animal);
  }
  makeAllSounds() {
    for (let pet of this.pets) {
      console.log(pet.sound());
    }
  }
}

var home = new Home();
var dog1 = new Dog();
var dog2 = new Dog();
var cat = new Cat();

home.makeAllSounds(); // this doesn't give/return any result/data
console.log("== Adding dog ==");
home.adoptPet(dog1);
home.makeAllSounds(); // this prints: Dog barks
console.log("== Adding cat ==");
home.adoptPet(cat);
home.makeAllSounds(); // this prints : Dog barks / Cat meows
console.log("== Adding dog ==");
home.adoptPet(dog2);
home.makeAllSounds(); // ...
home.adoptPet(dog1) // not ok!
 Run code snippet
Comment

Javascript Encapsulation Inheritance Polymorphism Composition

class Animal {
  eat() {
    return "Rax eat";
  }
}

class Dog extends Animal {
  sound() {
    return "Dog barks";
  }
}

class Cat extends Animal {
  sound() {
    return "Cat meows";
  }
}

class Home {
  constructor() {
    this.pets = []; // Could be a `new Set` for better efficiency
  }
  adoptPet(animal) {
    if (this.pets.includes(animal)) {
      throw new Error("Cannot adopt the same pet twice");
    }
    this.pets.push(animal);
  }
  makeAllSounds() {
    for (let pet of this.pets) {
      console.log(pet.sound());
    }
  }
}

var home = new Home();
var dog1 = new Dog();
var dog2 = new Dog();
var cat = new Cat();

home.makeAllSounds(); // this doesn't give/return any result/data
console.log("== Adding dog ==");
home.adoptPet(dog1);
home.makeAllSounds(); // this prints: Dog barks
console.log("== Adding cat ==");
home.adoptPet(cat);
home.makeAllSounds(); // this prints : Dog barks / Cat meows
console.log("== Adding dog ==");
home.adoptPet(dog2);
home.makeAllSounds(); // ...
home.adoptPet(dog1) // not ok!
 Run code snippet
Comment

Javascript Encapsulation Inheritance Polymorphism Composition

class Animal {
  eat() {
    return "Rax eat";
  }
}

class Dog extends Animal {
  sound() {
    return "Dog barks";
  }
}

class Cat extends Animal {
  sound() {
    return "Cat meows";
  }
}

class Home {
  constructor() {
    this.pets = []; // Could be a `new Set` for better efficiency
  }
  adoptPet(animal) {
    if (this.pets.includes(animal)) {
      throw new Error("Cannot adopt the same pet twice");
    }
    this.pets.push(animal);
  }
  makeAllSounds() {
    for (let pet of this.pets) {
      console.log(pet.sound());
    }
  }
}

var home = new Home();
var dog1 = new Dog();
var dog2 = new Dog();
var cat = new Cat();

home.makeAllSounds(); // this doesn't give/return any result/data
console.log("== Adding dog ==");
home.adoptPet(dog1);
home.makeAllSounds(); // this prints: Dog barks
console.log("== Adding cat ==");
home.adoptPet(cat);
home.makeAllSounds(); // this prints : Dog barks / Cat meows
console.log("== Adding dog ==");
home.adoptPet(dog2);
home.makeAllSounds(); // ...
home.adoptPet(dog1) // not ok!
 Run code snippet
Comment

Javascript Encapsulation Inheritance Polymorphism Composition

class Animal {
  eat() {
    return "Rax eat";
  }
}

class Dog extends Animal {
  sound() {
    return "Dog barks";
  }
}

class Cat extends Animal {
  sound() {
    return "Cat meows";
  }
}

class Home {
  constructor() {
    this.pets = []; // Could be a `new Set` for better efficiency
  }
  adoptPet(animal) {
    if (this.pets.includes(animal)) {
      throw new Error("Cannot adopt the same pet twice");
    }
    this.pets.push(animal);
  }
  makeAllSounds() {
    for (let pet of this.pets) {
      console.log(pet.sound());
    }
  }
}

var home = new Home();
var dog1 = new Dog();
var dog2 = new Dog();
var cat = new Cat();

home.makeAllSounds(); // this doesn't give/return any result/data
console.log("== Adding dog ==");
home.adoptPet(dog1);
home.makeAllSounds(); // this prints: Dog barks
console.log("== Adding cat ==");
home.adoptPet(cat);
home.makeAllSounds(); // this prints : Dog barks / Cat meows
console.log("== Adding dog ==");
home.adoptPet(dog2);
home.makeAllSounds(); // ...
home.adoptPet(dog1) // not ok!
 Run code snippet
Comment

Javascript Encapsulation Inheritance Polymorphism Composition

class Animal {
  eat() {
    return "Rax eat";
  }
}

class Dog extends Animal {
  sound() {
    return "Dog barks";
  }
}

class Cat extends Animal {
  sound() {
    return "Cat meows";
  }
}

class Home {
  constructor() {
    this.pets = []; // Could be a `new Set` for better efficiency
  }
  adoptPet(animal) {
    if (this.pets.includes(animal)) {
      throw new Error("Cannot adopt the same pet twice");
    }
    this.pets.push(animal);
  }
  makeAllSounds() {
    for (let pet of this.pets) {
      console.log(pet.sound());
    }
  }
}

var home = new Home();
var dog1 = new Dog();
var dog2 = new Dog();
var cat = new Cat();

home.makeAllSounds(); // this doesn't give/return any result/data
console.log("== Adding dog ==");
home.adoptPet(dog1);
home.makeAllSounds(); // this prints: Dog barks
console.log("== Adding cat ==");
home.adoptPet(cat);
home.makeAllSounds(); // this prints : Dog barks / Cat meows
console.log("== Adding dog ==");
home.adoptPet(dog2);
home.makeAllSounds(); // ...
home.adoptPet(dog1) // not ok!
 Run code snippet
Comment

Javascript Encapsulation Inheritance Polymorphism Composition

class Animal {
  eat() {
    return "Rax eat";
  }
}

class Dog extends Animal {
  sound() {
    return "Dog barks";
  }
}

class Cat extends Animal {
  sound() {
    return "Cat meows";
  }
}

class Home {
  constructor() {
    this.pets = []; // Could be a `new Set` for better efficiency
  }
  adoptPet(animal) {
    if (this.pets.includes(animal)) {
      throw new Error("Cannot adopt the same pet twice");
    }
    this.pets.push(animal);
  }
  makeAllSounds() {
    for (let pet of this.pets) {
      console.log(pet.sound());
    }
  }
}

var home = new Home();
var dog1 = new Dog();
var dog2 = new Dog();
var cat = new Cat();

home.makeAllSounds(); // this doesn't give/return any result/data
console.log("== Adding dog ==");
home.adoptPet(dog1);
home.makeAllSounds(); // this prints: Dog barks
console.log("== Adding cat ==");
home.adoptPet(cat);
home.makeAllSounds(); // this prints : Dog barks / Cat meows
console.log("== Adding dog ==");
home.adoptPet(dog2);
home.makeAllSounds(); // ...
home.adoptPet(dog1) // not ok!
 Run code snippet
Comment

Javascript Encapsulation Inheritance Polymorphism Composition

class Animal {
  eat() {
    return "Rax eat";
  }
}

class Dog extends Animal {
  sound() {
    return "Dog barks";
  }
}

class Cat extends Animal {
  sound() {
    return "Cat meows";
  }
}

class Home {
  constructor() {
    this.pets = []; // Could be a `new Set` for better efficiency
  }
  adoptPet(animal) {
    if (this.pets.includes(animal)) {
      throw new Error("Cannot adopt the same pet twice");
    }
    this.pets.push(animal);
  }
  makeAllSounds() {
    for (let pet of this.pets) {
      console.log(pet.sound());
    }
  }
}

var home = new Home();
var dog1 = new Dog();
var dog2 = new Dog();
var cat = new Cat();

home.makeAllSounds(); // this doesn't give/return any result/data
console.log("== Adding dog ==");
home.adoptPet(dog1);
home.makeAllSounds(); // this prints: Dog barks
console.log("== Adding cat ==");
home.adoptPet(cat);
home.makeAllSounds(); // this prints : Dog barks / Cat meows
console.log("== Adding dog ==");
home.adoptPet(dog2);
home.makeAllSounds(); // ...
home.adoptPet(dog1) // not ok!
 Run code snippet
Comment

Javascript Encapsulation Inheritance Polymorphism Composition

class Animal {
  eat() {
    return "Rax eat";
  }
}

class Dog extends Animal {
  sound() {
    return "Dog barks";
  }
}

class Cat extends Animal {
  sound() {
    return "Cat meows";
  }
}

class Home {
  constructor() {
    this.pets = []; // Could be a `new Set` for better efficiency
  }
  adoptPet(animal) {
    if (this.pets.includes(animal)) {
      throw new Error("Cannot adopt the same pet twice");
    }
    this.pets.push(animal);
  }
  makeAllSounds() {
    for (let pet of this.pets) {
      console.log(pet.sound());
    }
  }
}

var home = new Home();
var dog1 = new Dog();
var dog2 = new Dog();
var cat = new Cat();

home.makeAllSounds(); // this doesn't give/return any result/data
console.log("== Adding dog ==");
home.adoptPet(dog1);
home.makeAllSounds(); // this prints: Dog barks
console.log("== Adding cat ==");
home.adoptPet(cat);
home.makeAllSounds(); // this prints : Dog barks / Cat meows
console.log("== Adding dog ==");
home.adoptPet(dog2);
home.makeAllSounds(); // ...
home.adoptPet(dog1) // not ok!
 Run code snippet
Comment

Javascript Encapsulation Inheritance Polymorphism Composition

class Animal {
  eat() {
    return "Rax eat";
  }
}

class Dog extends Animal {
  sound() {
    return "Dog barks";
  }
}

class Cat extends Animal {
  sound() {
    return "Cat meows";
  }
}

class Home {
  constructor() {
    this.pets = []; // Could be a `new Set` for better efficiency
  }
  adoptPet(animal) {
    if (this.pets.includes(animal)) {
      throw new Error("Cannot adopt the same pet twice");
    }
    this.pets.push(animal);
  }
  makeAllSounds() {
    for (let pet of this.pets) {
      console.log(pet.sound());
    }
  }
}

var home = new Home();
var dog1 = new Dog();
var dog2 = new Dog();
var cat = new Cat();

home.makeAllSounds(); // this doesn't give/return any result/data
console.log("== Adding dog ==");
home.adoptPet(dog1);
home.makeAllSounds(); // this prints: Dog barks
console.log("== Adding cat ==");
home.adoptPet(cat);
home.makeAllSounds(); // this prints : Dog barks / Cat meows
console.log("== Adding dog ==");
home.adoptPet(dog2);
home.makeAllSounds(); // ...
home.adoptPet(dog1) // not ok!
 Run code snippet
Comment

Javascript Encapsulation Inheritance Polymorphism Composition

class Animal {
  eat() {
    return "Rax eat";
  }
}

class Dog extends Animal {
  sound() {
    return "Dog barks";
  }
}

class Cat extends Animal {
  sound() {
    return "Cat meows";
  }
}

class Home {
  constructor() {
    this.pets = []; // Could be a `new Set` for better efficiency
  }
  adoptPet(animal) {
    if (this.pets.includes(animal)) {
      throw new Error("Cannot adopt the same pet twice");
    }
    this.pets.push(animal);
  }
  makeAllSounds() {
    for (let pet of this.pets) {
      console.log(pet.sound());
    }
  }
}

var home = new Home();
var dog1 = new Dog();
var dog2 = new Dog();
var cat = new Cat();

home.makeAllSounds(); // this doesn't give/return any result/data
console.log("== Adding dog ==");
home.adoptPet(dog1);
home.makeAllSounds(); // this prints: Dog barks
console.log("== Adding cat ==");
home.adoptPet(cat);
home.makeAllSounds(); // this prints : Dog barks / Cat meows
console.log("== Adding dog ==");
home.adoptPet(dog2);
home.makeAllSounds(); // ...
home.adoptPet(dog1) // not ok!
 Run code snippet
Comment

Javascript Encapsulation Inheritance Polymorphism Composition

class Animal {
  eat() {
    return "Rax eat";
  }
}

class Dog extends Animal {
  sound() {
    return "Dog barks";
  }
}

class Cat extends Animal {
  sound() {
    return "Cat meows";
  }
}

class Home {
  constructor() {
    this.pets = []; // Could be a `new Set` for better efficiency
  }
  adoptPet(animal) {
    if (this.pets.includes(animal)) {
      throw new Error("Cannot adopt the same pet twice");
    }
    this.pets.push(animal);
  }
  makeAllSounds() {
    for (let pet of this.pets) {
      console.log(pet.sound());
    }
  }
}

var home = new Home();
var dog1 = new Dog();
var dog2 = new Dog();
var cat = new Cat();

home.makeAllSounds(); // this doesn't give/return any result/data
console.log("== Adding dog ==");
home.adoptPet(dog1);
home.makeAllSounds(); // this prints: Dog barks
console.log("== Adding cat ==");
home.adoptPet(cat);
home.makeAllSounds(); // this prints : Dog barks / Cat meows
console.log("== Adding dog ==");
home.adoptPet(dog2);
home.makeAllSounds(); // ...
home.adoptPet(dog1) // not ok!
 Run code snippet
Comment

Javascript Encapsulation Inheritance Polymorphism Composition

class Animal {
  eat() {
    return "Rax eat";
  }
}

class Dog extends Animal {
  sound() {
    return "Dog barks";
  }
}

class Cat extends Animal {
  sound() {
    return "Cat meows";
  }
}

class Home {
  constructor() {
    this.pets = []; // Could be a `new Set` for better efficiency
  }
  adoptPet(animal) {
    if (this.pets.includes(animal)) {
      throw new Error("Cannot adopt the same pet twice");
    }
    this.pets.push(animal);
  }
  makeAllSounds() {
    for (let pet of this.pets) {
      console.log(pet.sound());
    }
  }
}

var home = new Home();
var dog1 = new Dog();
var dog2 = new Dog();
var cat = new Cat();

home.makeAllSounds(); // this doesn't give/return any result/data
console.log("== Adding dog ==");
home.adoptPet(dog1);
home.makeAllSounds(); // this prints: Dog barks
console.log("== Adding cat ==");
home.adoptPet(cat);
home.makeAllSounds(); // this prints : Dog barks / Cat meows
console.log("== Adding dog ==");
home.adoptPet(dog2);
home.makeAllSounds(); // ...
home.adoptPet(dog1) // not ok!
 Run code snippet
Comment

PREVIOUS NEXT
Code Example
Javascript :: Object methods + Static methods javascript 
Javascript :: React Using Self Made Module 
Javascript :: Self Invoking Function Tip 
Javascript :: underscore js filter array of objects 
Javascript :: phaser add camera 
Javascript :: javascript reverse string short hand 
Javascript :: call axios post with an interval 
Javascript :: javascript complier 
Javascript :: NodeJS Database initialisation 
Javascript :: cubing timer 
Javascript :: Javascript twoSum algorithm: Given an array of integers, return indices of the two numbers such that they add up to a specific target 
Javascript :: nested object data 
Javascript :: var logNums = function(num) {}; 
Javascript :: json to dart dummy api 
Javascript :: NavBar with divs 
Javascript :: leetcode solution problem 66 plus one 
Javascript :: Backbone Model And Collection 
Javascript :: Javascript set control state none opposite 
Javascript :: shrinkStringByRepeatFactor 
Javascript :: Solution-1--solution options for reverse bits algorithm js 
Javascript :: react native componentdidmount in function 
Javascript :: object.map() nest js 
Javascript :: javascript array cheatsheet 
Javascript :: react component did mount function 
Javascript :: how to remove duplicates in js 
Javascript :: how to check if input field has value 
Javascript :: client position js 
Javascript :: The DOM Parent-Child Relationship 
Javascript :: JavaScript Add Methods to a Constructor Function Using Prototype 
Javascript :: javascript setTimeout() method returns the interval id 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =