Guest User

Untitled

a guest
Apr 20th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. // use static methods as utility functions that are not tied to a specific instance of a class
  2.  
  3. // obligatory animal example
  4. class Dog {
  5. constructor(breed) {
  6. this.breed = breed;
  7. }
  8.  
  9. speak() {
  10. return `I am a ${this.breed} dog. Woof!`;
  11. }
  12.  
  13. static compareBreeds(dog1, dog2) {
  14. return (dog1.breed === dog2.breed);
  15. }
  16.  
  17. }
  18.  
  19. let bigDog = new Dog("Great Dane");
  20. let anotherBigDog = new Dog("Great Dane");
  21. let smallDog = new Dog("Chihuahua");
  22.  
  23. console.log(Dog.compareBreeds(bigDog, smallDog)); // false
  24. console.log(Dog.compareBreeds(bigDog, anotherBigDog));
  25. console.log(bigDog.speak());
  26. console.log(smallDog.speak());
  27. // What will happen when you uncomment the line below?
  28. //console.log(bigDog.compareBreeds(bigDog, smallDog));
Add Comment
Please, Sign In to add comment