Table of Contents
Back Button
Back to Chapter
Control Statements
No items found.
Function & Events
No items found.
Array and its Types
No items found.
OOP : Object Oriented Programming
No items found.
Javascript Standards
No items found.
HTML DOM
No items found.
Javascript : Advanced 1
No items found.
Javascript : Advanced 2
No items found.
Additional JavaScript
No items found.

Classes

What is Class?

A Class is a Program Template for Creating Object, having similar Properties.

For Example: Consider the Class of Cars. There may be many cars with different names and brands but all of them will share some common properties like all of them will have 4 wheels, Speed Limit, Mileage range, etc. So here, Car is the class, and wheels, speed limits, mileage are their properties.

Class Declaration

Syntax:

class className { constructor(parameters (if any)) { // statements } method1(parameters (if any)){ // statements } method2(parameters (if any)){ // statements } . . . }

Here,

Constructor is a method that need to be defined after defining a className

It is Executed automatically when a new object is created , we can use it to initialize object properties.

Methods are used as a function. There can be any number of Methods.

Example :

class identity{ constructor(name, age) { this.name = name; this.age = age; } printDetails(){ return "I am " + this.name + " and i am "+ this.age + " years old."; } } let person = new identity("John Doe","19"); console.log(person.printDetails()); // Output // I am John Doe and i am 19 years old.

Here,

We have declared a class with name identity .

Then, We are using declaring a variable person and creating a new object identity , and calling a class assigned variable person and calling printDetails() method

This Method is returning a Identity String that is printing in the Console Tab.

Ask queries
Contact Us on Whatsapp
Hi, How Can We Help You?