Front end Development 2 Prototypes - Vincentvanleeuwen/project-tech-2020 GitHub Wiki

Prototypes in JavaScript

Theres different kind of prototypes. You've got the Object.prototype, Function.prototype and Array.prototype.

Each of these prototypes got a set of functions that you can use. For instance, you can use pop() on an array to delete the last entry.

But you can also create your own prototypes.

Lets say you have an object named "dog" that contains the color "beige" and the breed "labrador"

let dog = {

breed: "labrador"

color: "beige";

}

You can then create a new function for this dog by using the .prototype on the dog and naming your new function, in this instance that is "getBreed"

dog.prototype.getBreed = function() {

return this.breed

}

You can now use that function by calling dog.getBreed(); It will print: "labrador"

source: https://medium.com/backticks-tildes/javascript-prototypes-ee46810e4866