Information hiding in ES6 classes

Implementing the information hiding principle using the ES6 syntax enhancements is not so different. As we said when introducing them, ES6 classes are nothing more than a new syntactic approach to define constructors and methods for our objects. The major benefits include a more concise syntax and the application of the best practices in the internal implementation of object creation.

So, in order to correctly protect our private members, we need again to use an IIFE in order to exploit its closure and WeakMaps to store private members. The following is a definition of the TheatreSeats class with private members protection:

var TheatreSeats = (function() { "use strict"; var priv = new WeakMap(); var _= function(instance) ...

Get Mastering JavaScript Object-Oriented Programming 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.