Blog

JavaScript/ES6 Private Methods in class without closure using Symbol

 982 total views,  1 views today

The data type symbol is a primitive data type. The Symbol() function returns a value of type symbol, has static properties that expose several members of built-in objects, has static methods that expose the global symbol registry, and resembles a built-in object class, but is incomplete as a constructor because it does not support the syntax “new Symbol()“.  

Every symbol value returned from Symbol() is unique.  

A symbol value may be used as an identifier for object properties; this is the data type’s primary purpose, although other use-cases exist, such as enabling opaque data types, or serving as an implementation-supported unique identifier in genera

TIP: A string and symbol are not the same thing.

You can quickly create symbol by using the following code block.

let sym = Symbol('foo');   // returns a unique value
typeof sym      // "symbol" 

Symbol and string are not the same.

console.log(sym === "foo"); // Returns false

Also, you can verify this by the below code block. (a Symbol always returns a unique value)

let id1 = Symbol("id");
let id2 = Symbol("id");

console.log(id1 == id2); // false

Let’s now create private methods in ES6 class using Symbol

Private Method using Symbol

Here we are simulating the structure for an Alert library. The library itself is very insignificant.

Here we want to expose a public method “show” which the client can call and also other methods should be hidden from the client, for e.g. setting up events. etc.

Here we use two important feature of JavaScript, Symbol and Computed property to create dynamic method.

Let’s peek into the code.

console.clear();

(function () {
  const SETUP_EVENTS = Symbol.for("events");  

  class Alert  {
    constructor(message) {
      this.message = message;
      this[SETUP_EVENTS]();  // accessible within the class
    }

    // YOUR EYES SHOULD BE HERE (This method cannot be called by the client)
    [SETUP_EVENTS]() {
      console.log("Setting up events!!!");
    }

    // Public method
    show() {
      console.log(this.message);
    }
  }
  
  window.Alert = Alert;
}());

let success = new Alert("Success!!");
success.show();

Code Explanation

Let’s understand the important aspect of the above code.

  • We first wrap our code in a IIFE so that the variables are not leaked to global implicitly. (SO, our symbols are not accessible outside this IIFE)
  • We create a unique symbol for events using Symbol.for syntax
  • We then use the computed property syntax to create our private method
  • The show method is public and the client can use it, but the client in no way can access the setup events method.

This was a quick tip and if you liked this, do comment.

I haven’t covered in depth many other aspects of Symbol (take a look at MDN).

How useful was this post?

Click on a heart to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Leave a Reply