Classes And Objects In Js

Posted on August 6, 2025 by Vishesh Namdev
Python C C++ Javascript Java
Classes and Objects in JS

Classes and Objects in JS

Classes and objects are a fundamental concept in object-oriented programming (OOP). In JavaScript, classes and objects are used to create reusable code and promote modularity. In this article, we will explore the basics of classes and objects in JavaScript.

What is an Object?

An object is an instance of a class. It has its own set of attributes (data) and methods (functions) that can be used to interact with the object. Objects are the basic building blocks of OOP's, and they are used to model real-world objects and their interactions.

What is a Class?

A class is a blueprint or a template that defines the properties and methods of an object. It is a way to define a set of attributes and methods that can be used to create objects. Classes are the foundation of OOP's, and they are used to model real-world objects and their interactions.

class RailwayForm{
       submit(){
           alert(this.name + " Form Submitted" + " for this Train no." + this.trainNo);
            }
       cancel(){
            alert(this.name + " Form Cancelled" + " for this Train no." + this.trainNo);
            }
       fill(givenname, trainno){
            this.name = givenname;
            this.trainNo = trainno
            }
        }
   let rohanForm = new RailwayForm()
   rohanForm.fill("Rohan", 12210)

   let mohanForm1 = new RailwayForm()
   let mohanForm2 = new RailwayForm()
   mohanForm1.fill("Mohan", 11110)
   mohanForm2.fill("Mohan", 1213340)

   mohanForm1.submit()
   rohanForm.submit()
   mohanForm2.submit()
   mohanForm1.cancel()

Code Overview

This code defines a class called RailwayForm to simulate filling, submitting, and canceling a train reservation form.

Class Declaration:
class RailwayForm {
  • This defines a class named RailwayForm.
  • A class is a template or blueprint to create multiple objects with the same properties and methods.
  • Method: submit()
    submit() {
        alert(this.name + " Form Submitted" + " for this Train no." + this.trainNo);
    }
    
  • This method displays an alert when a form is submitted.
  • It uses this.name and this.trainNo, which are expected to be properties of the object.
  • this refers to the current object calling the method.
  • Method: cancel()
    cancel() {
        alert(this.name + " Form Cancelled" + " for this Train no." + this.trainNo);
    }
    
  • Similar to submit(), but for cancellation.
  • Shows an alert with the name and train number when the form is canceled.
  • Method: fill(givenname, trainno)
    fill(givenname, trainno) {
        this.name = givenname;
        this.trainNo = trainno;
    }
  • This method is used to set or assign values to name and trainNo for an object.
  • givenname and trainno are input parameters.
  • Creating and Filling Object: rohanForm
    let rohanForm = new RailwayForm();
    rohanForm.fill("Rohan", 12210);
  • A new object rohanForm is created from the RailwayForm class.
  • .fill("Rohan", 12210) sets:
    rohanForm.name = "Rohan"
    rohanForm.trainNo = 12210
  • Creating Two Forms for Mohan
    let mohanForm1 = new RailwayForm();
    let mohanForm2 = new RailwayForm();
  • Two separate objects created from the same class: mohanForm1 and mohanForm2.
  • mohanForm1.fill("Mohan", 11110);
    mohanForm2.fill("Mohan", 1213340);
  • Assign values using .fill():
  • mohanForm1 gets "Mohan" and 11110.
  • mohanForm2 gets "Mohan" and 1213340.
  • ๐Ÿ“ขImportant Note๐Ÿ“ข

    How did you feel about this post?

    ๐Ÿ˜ ๐Ÿ™‚ ๐Ÿ˜ ๐Ÿ˜• ๐Ÿ˜ก

    Was this helpful?

    ๐Ÿ‘ ๐Ÿ‘Ž