Course Banner


JavaScript Classes

One of the important features

ECS6 is it is object-oriented.

These Classes are actually templates for JavaScript Objects.

‘Class’ keyword is used to declare a javascript class.

A default method constructor() is used.

Syntax

class ClassName {

constructor() { ... }

}

Example

class Car {

constructor(carName, purchasedYear) {

this.carName = carName;

this.purchasedYear = purchasedYear;

}

}

 

In the above example a class called ‘Car’ has been declared

. A constructor is also declared. Two initial properties

called carName and purchasedYea have been declared.

A JavaScript class is not an object.

It is a template for JavaScript objects.

Bear in mind that in Javascript everything is ‘object’. But class is not
an object. Its template for javascript.

Class Example :

If you want to access properties or method of the class then you

need to create an object of that class.

In the above example, we have created a class called

Byke, we have two properties ByName and launchYear.

Later we created an object of that class called myByke.

Now properties of the class can be accessed with that

class name
We have passed Bullet 250 and 2018 as parameters.