Prototypal Inheritance (Old Way) function Animal(name) { this.name = name; } Animal.prototype.sayHello = function() { console.log("Hello, I'm " + this.name); }; let dog = new Animal("Dog"); dog.sayHello(); // Output: Hello, I'm Dog Classes (New Way – ES6) class Animal { constructor(name) { this.name = name; } sayHello() { console.log(`Hello, I'm ${this.name}`); } } let cat = new Animal("Cat"); cat.sayHello(); // Output: Hello, I'm Cat
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter