Revealing Constructor

Learn about the revealing constructor and how to build an immutable buffer.

We'll cover the following

The Revealing Constructor pattern is one of those patterns that you won’t find in the “Gang of Four” book because it originated directly from the JavaScript and the Node.js community. It solves a very tricky problem, which is: how can we “reveal” some private functionality of an object only at the moment of the object’s creation? This is particularly useful when we want to allow an object’s internals to be manipulated only during its creation phase. This allows for a few interesting scenarios, such as the following:

  • Creating objects that can be modified only at creation time

  • Creating objects whose custom behavior can be defined only at creation time

  • Creating objects that can be initialized only once at creation time

These are just a few possibilities enabled by the Revealing Constructor pattern. To better understand all the possible use cases, let’s see what the Revealing Constructor pattern is about by looking at the following code fragment:

Get hands-on with 1200+ tech skills courses.