Classes Function and it’s Type in JS

Snehil Verma
Snehil Verma
Published in
2 min readJan 31, 2020

I have been using javascript classes and function for a while. I came across two types of function with the class that is, the normal function and the static function. Let’s see what is the difference between the two types of function with classes in the javascript.

The static function can be written in the following way:-

class abc {
constructor(x) {
this.x = x;
}
static display(x) {
console.log("value of x is:", x);
}
}

In the object-oriented language, the static members always belong to the class. Here we don’t have the class so let’s see how does the behaviour change. If we create a new object of the class abc then display function will belong to the constructor on the object.

let test = new abc(5);

Now if you look at the prototype of the test then it will be like the image below:-

Here you may see that the function display() belong to the constructor.

Now let’s have a look at the normal function example:-

class abc1 {
constructor(x) {
this.x = x;
}
display() {
console.log(this.x);
}
}

Here we see that the display is not a static function and now if the object of abc1 is created like let test1 = new abc1(5) . The test1 object prototype will be something like the image below:-

In the image above the display() function belongs to the object abc1 and not to the constructor of the object so it may be called by the test1.display() .

You will get a better understanding of function, classes and it’s working if you understand prototype and it’s working in javascript.

--

--