JavaScript ES6+ : Syntaxes for implementing private methods, private properties, static properties and static methods in JavaScript class

In this article i am going to explain how to use private methods, properties and static methods and properties in ES6+ based JavaScript class. If you have previous programming experience with OOP (Object Oriented Programming) Language, you may have used private, public and static methods and properties. Today we will look at how the same functionality is achieved with JavaScript.

Lets start our article here.

 

Private methods.

private methods are used only within the class and they are intended to be used by the methods within the class. since the access level is private, those methods cannot be invoked from outside of the application.

 

Why do we need private methods?

First of all we should look at the reason behind the need of the private methods. Those reasons can be described as follows.

  • prevent the code duplication

In your software development life, you might have seen that there are some common logic in a class that need to be reused in more than one method of the same class. So if we do not separate those logic/codings into a separate method, we have to duplicate them in every method. In such case we might need a private method.

  • Improve the code readability and cleanliness. 

Sometimes you might have noticed that there some methods which are lengthy due to many number of code lines. Therefore it is very difficult to read the method contents. Therefore the readability of the method goes down. If you observe those methods carefully,  you can find some similar set of logics that can be separated into groups. So separating those similar logics (groups) into a private methods, may improve the readability of your source code.

Therefore it is good idea to separate and group the similar logic into a private methods which will improve the cleanliness, reusability and readability of your source code.

Ok. enough theories.  Lets look at some coding examples here.

for the demonstration purpose, i will use simple example from java programming language and convert it into the the JavaScript ES6. Then you will be able to understand them easily by comparing each of them.

 

Private members 

First we will look at how to add a private property (member variable) for a class. In java, this can be simply achieved as follows.

 

public class Sample
{
 private String privateProperty;

 public String getPrivateProperty(){
   return privateProperty;
 }
}

 

The same thing can be achieved with JavaScript ES6 is as follows.

let  privateProperty;

class Sample
{
    getPrivateProperty()
    {
        return privateProperty;
    }
}

 

If you want to declare a private property in ES6, it should be declared outside of the class.  Then even if the class is instantiated, the private property cannot be accessed because, it does not belong to the scope of the class.

 

Static properties and methods

In java we can add static members and static methods as follows.

public class Sample
{
    static String staticProperty;
 
    //getter for the static property
    public String getPrivateProperty()
    {
        return staticProperty;
    }

    //another static method 
    public static String sayHello(){
        return "hello";
    }
}

 

The same thing can be achieved with javascript as follows.

let staticProperty;

class Sample
{
    //getter for the staticProperty
    static getStaticProperty()
    {
        return staticProperty;
    }

    //we can add static modifier for methods to make them as static methods
    static sayHello()
    {
      return "hello";
    }
}

 

In java we can  add the static modifier to member variable for making it as a static field. But in javascript, we cannot add such modifiers.  The only way to achieve static filed is to add a static getter for the field the need to be set as static.

static modifier can be added for the methods to make them as static.

 

Private methods

In java private methods can be declared with private access modifier. please refer the below code segment.

public class Sample
{
    public String displayWelcomeMessage(){
        return this.sayHello();
    }

    private String sayHello(){
        return "hello";
    }
}

 

But in javascript, you have to declare it as follows.

let methods = {
    //the multiple private methods can be added here (by comma seperated)
    sayHello(){
        return "hello";
    }
};

class Sample {
    displayWelcomeMessage() {
        return methods.sayHello();
    }
}

 

 

After reading this article, i believe that you got a good understanding of how to declare private and static fields(members) in javascript ES6. In addition, you should have a good understanding of how to declare static methods in a class. In you need any clarification regarding this article, feel free to contact me.

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s