Encapsulate Truly Private Variables in a Closure

Share this video with your friends

Send Tweet

Closures are an alternative to truly private class fields. If you need to encapsulate a private variable so that it can only be accessed in a controlled way, you can use a closure. This technique is particularly useful when your code has to run in older JavaScript environments that don't yet support truly private class fields or the ES2015 WeakMap data structure.