JavaScript Classes

Hello! I hope you had a good day and an awesome week. 😊

In this post, I thought of talking about JavaScript classes. Before we talk about JavaScript classes, we need to know what JavaScript is. I know you all know what that is, but just in case if you have forgotten I’ll give you a brief introduction.

JavaScript is a scripting language that can be used to client-side processing and can be inserted into HTML pages. Some of its major features are,
·         Open-source
·         Single threaded
·         Non-blocking I/O
·         Asynchronous
·         Dynamically typed
·         Multi-paradigm
·         Event driven

JavaScript doesn’t have classes. It is a class-less language. Everything is considered as objects. Even in inheritance an object will inherit from another object. There are 3 methods to define a class in JavaScript.

1. Using a Function

This is the most common way to define a class in JavaScript. A normal JavaScript function which we call as a constructor function is define and the ‘new’ keyword is used when creating an object.



After defining the class, object can be created and attributes can be accessed as follows. If necessary, attribute values can be changed as well.


In this above example the way of defining a class and attributes is shown. To define a method for the class there are 3 different ways.

The method can be defined outside the constructor function as a separate function. But this has one drawback. As all the functions are defined in the global namespace, naming conflicts might arise.


The method can be defined inside the constructor function. The problem here is that the defined methods will be created every time a new object is created.


The method can be added to the prototype. Here the constructor function has a reference to another object called ‘prototype’. So, all the object instances of defined class will refer the same prototype object.


The way of instantiating the object is same for all three mentioned ways of defining a method. Prototypes are the most recommended way of defining a method.

2. Using Object Literals

This is a shorter way to define classes in JavaScript.


In this case an instance of the class can’t be created because it already exists. So right away the instance can be used.


This kind of an object is called ‘Singleton’. In other languages, singleton means only one instance of a class can be created. This makes no sense in JavaScript as all the objects are singleton. When we define a class from a constructor function, what we do is making several copies of the same object.

 3. Object Literals with a Function

This is a combination of the previous two methods. A function can be used to define a singleton object.


In this way also an instance can’t be created.

Those are the three ways to define a class in JavaScript due its flexibility.


I really hope you learnt something. I wish you all an amazing week ahead. Adios 😊

Comments

Popular posts from this blog

Double Submit Cookies Pattern - Node.js

Synchronizer Token Pattern - Node.js

Implementing an OAuth 2.0 Client - Node.js