loading...

The Prototype Pattern in JavaScript

jsmanifest on November 20, 2019

Find me on medium There are multiple design patterns that can be implemented in the JavaScript language, and in this post we will be going over th... [Read Full]
markdown guide
 

..which is equivalent to the code below, a typical class object in modern javascript. I prefer this over modifying prototype objects directly, just because it looks cleaner.

class Warrior { 
    constructor(name) {
        this.name = name;
        this.hp = 100;
    }
    bash(target) {
        target.hp -= 15;
    }
    omniSlash(target) {
        if(target.hp < 50) {
            return;
        }
        target.hp -= 50;
    }
}
 

Very informational read, never understood why some experienced devs used prototypes, now I do and plan on doing the same. Thanks a lot for shedding light on the subject

 
code of conduct - report abuse