Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // use static methods as utility functions that are not tied to a specific instance of a class
- // obligatory animal example
- class Dog {
- constructor(breed) {
- this.breed = breed;
- }
- speak() {
- return `I am a ${this.breed} dog. Woof!`;
- }
- static compareBreeds(dog1, dog2) {
- return (dog1.breed === dog2.breed);
- }
- }
- let bigDog = new Dog("Great Dane");
- let anotherBigDog = new Dog("Great Dane");
- let smallDog = new Dog("Chihuahua");
- console.log(Dog.compareBreeds(bigDog, smallDog)); // false
- console.log(Dog.compareBreeds(bigDog, anotherBigDog));
- console.log(bigDog.speak());
- console.log(smallDog.speak());
- // What will happen when you uncomment the line below?
- //console.log(bigDog.compareBreeds(bigDog, smallDog));
Add Comment
Please, Sign In to add comment