JavaScript Jems - The Revealing Constructor Pattern
Written by Mike James   
Thursday, 27 March 2025
Article Index
JavaScript Jems - The Revealing Constructor Pattern
Private Methods
The Revealing Constructor

The Revealing Constructor

The solution to the mistake is simple enough – pass the required private members as parameters to the function. This is what you do whenever you need a function to work with values that exist but are not in scope - that's what parameters are for. For example:

var myObject = new myClass(
        function (reveal) { console.log(reveal()); }
       );

Note that the function needs to be called within the constructor with a parameter:

function myClass(func) {
  var myPrivateVar = Math.random();
  var reveal = function () { return myPrivateVar;};
  func(reveal);
}

Now it all works but it looks very strange.

When you look at the constructor call you get the uneasy feeling that it is wrong because reveal hasn't been defined - it isn't in scope. However, reveal in this case is a parameter in the function definition and nothing to do with any reveal that is within the constructor.

The function passed to the constructor can call private functions within the constructor as long as they are passed to it when the function is called.

Notice that you can just as well write:

var myObject = new myClass(
                function (firstParameter) {
                    console.log(firstParameter());}
               );

The name used for the first parameter is irrelevant just like that of any parameter – it only has meaning within the function that it is a parameter of. The value of the first parameter, i.e. the function to be passed in, only becomes fixed when the constructor calls the function. That is, when it reaches:

func(reveal);

Notice that, after the constructor has finished its job, no other function can call reveal except for methods of the object constructed. It is private to the object and only accessible by closure.

The general principles of the revealing constructor pattern are:

  • Local variables, and hence inner functions within an object’s constructor, are private to the constructor and the object it constructs.

  • The object gains access to these private variables by closure.

  • Private members can be accessed by the code that calls the constructor by passing parameters.

  • If the code that calls the constructor passes a function that is executed by the constructor, it cannot access the private variables because they were not in scope when the function was defined.

  • Private variables can be passed to the function as they are in scope when the constructor calls the function.

The revealing constructor pattern is generally useful to allow external code restricted access to private functions within a constructor. It is, of course, how the Promise constructor allows you access to the resolve and reject functions - but only within the constructor – and this makes it a jem.

Now available as a book from your local Amazon.

JavaScript Jems:
The Amazing Parts

kindlecover

Contents

<ASIN:1871962579>

<ASIN:1871962560>

<ASIN:1871962501>

<ASIN:1871962528>

espbook

 

Comments




or email your comment to: comments@i-programmer.info

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.



Last Updated ( Saturday, 29 March 2025 )