Securely Implementing Protected Properties In Modern ES2022 JavaScript

Опубликовано: 01 Август 2026
на канале: Brian Katzung
14
0

In February 2026, most search results for implementing JavaScript protected properties are still suggesting using the underscore convention (with zero language-level enforcement) or WeakMaps (requiring classes to be located within the same lexical (file) scope), even though a better alternative has been possible for years.

In this video, I walk through and compare the different approaches, including a modern, ES2022-based approach built on synchronized private fields.

Table of contents
00:00 Intro, background, and goals
00:54 Historical: underscore convention
02:04 Historical: WeakMaps (per property)
03:14 Historical: explanation of demo output
04:34 Historical: single WeakMap (state object) [no audio]*
04:44 Private fields, passing state bottom-up
08:08 Bottom-up explanation of demo output
09:08 Top-down approach: benefits and issues
12:29 Top-down: explanation of demo output
13:03 Fixing top-down with setter-function subscriptions
21:53 Talking through the demo methods
22:50 Explanation of demo output
25:10 Summary of intra- and inter-instance access
25:38 Chart: comparison of protected-property implementations [no audio]

I somehow overlooked the option to group protected properties into a single state object (the same approach I use later) under the WeakMap model when preparing my original content. I've inserted a screen shot of this model variant at 4:34 without commentary.

You can find an implementation of the synchronized private-field approach on my GitHub at: https://github.com/bkatzung/protected-js.

I also have a blog post at https://www.kappacs.com/securely-impl....

#javascript #objectorientedprogramming #encapsulation