Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Instead of creating a duplicate of the property each time, we can simply add the property to the prototype, since all instances have access to the prototype object.

class Dog {
  constructor(name) {
    this.name = name;
  }

  bark() {
    return `Woof!`;
  }
}

const dog1 = new Dog("Daisy");
const dog2 = new Dog("Max");
const dog3 = new Dog("Spot");

Dog.prototype.play = () => console.log("Playing now!");

dog1.play();
Source by www.patterns.dev #
 
PREVIOUS NEXT
Tagged: #Instead #creating #duplicate #property #simply #add #property #instances #access #prototype
ADD COMMENT
Topic
Name
6+8 =