Summary

In this chapter, we explored how JavaScript allows us to implement encapsulation and information hiding principles. We used an incremental approach that allows us to analyze the various techniques currently adopted by most developers to protect the private members of an object. We started by analyzing a very simple technique based on a property naming convention and showed its benefits and drawbacks. Then, we described the privacy levels approach and then defined a better approach that uses WeakMaps.

Along this walk-through, we introduced a couple of useful concepts and our solutions were based on closures and Immediate Invoked Function Expressions (IIFE).

In addition to private member protection, we explored how to control access to public ...

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.