© Russ Ferguson and Keith Cirkel 2017

Russ Ferguson and Keith Cirkel, JavaScript Recipes, 10.1007/978-1-4302-6107-0_17

17. Working with Classes

Russ Ferguson and Keith Cirkel2

(1)Ocean, New Jersey, USA

(2)London, UK

How Do You Make a Class in JavaScript?

Problem

How do you create a class using either ECMAScript 5 or ECMAScript 6?

Solution

JavaScript is a prototype-based language, therefore its use of inheritance is also prototype-based. ECMAScript 5 uses functions to make classes. ECMAScript 6 introduces the class keyword as syntactical sugar for class creation in JavaScript.

The Code

Listing 17-1. Creating an ES5 Class and an ES6 Class
//ECMAScript 5 classvar Human = (        function Human(name){                 this.name = name;        })Human.prototype.sayGoodNight ...

Get JavaScript Recipes: A Problem-Solution Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.