Classes in JavaScript

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

Classes are the new buzz in Javascript ES6. Everyone is now using classes in their codes. Have you ever thought that even though the class have been introduced Javascript is still an object-based language and not an object-oriented language? I assume you must have the knowledge or prototypes in Javascript. How do classes work? Let’s understand it with a small.

class dummy {
constructor(sound) {
this.sound = sound;
}
makeSound() {
console.log("speaking: " + this.sound);
}
}

If you look at the code it is a simple class that has a constructor which accepts a parameter in it. It contains a function makeSound that displays the sound that is assigned to the object. Let’s create a new object from the class -

let dog = new dummy("bark");

Now if we do dog.makeSound() the output on the console will be “speaking: bark”.

But what if do console.log(dog) you will see that it will be an object with property sound in it. It will also have its prototype and inside the prototype, there will be a constructor and a function named makeSound . That is the basic representation of the function that has been converted into the object using the new keyword. Here is the complete representation of the object —

An object created from the class

So this can be concluded by saying that, classes in Javascript are just the wrapper that is created on the functions, prototype and objects so that they make javascript a bit easy to write and understand.

--

--