js class

JS Class

JavaScript is an object-oriented programming language that provides support for classes and objects. A class is a blueprint for creating objects that share common properties and methods. It defines the characteristics and behavior of an object. A class can be considered as a template for creating objects.

In JavaScript, a class is defined using the class keyword. The class keyword is followed by the name of the class, which should start with a capital letter. The properties and methods of the class are defined inside the class block.

Defining a JS Class


class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
  
  greeting() {
    console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
  }
}

The above code defines a class called "Person" that has two properties - "name" and "age" - and one method - "greeting". The constructor method is used to initialize the properties of the object when it is created.

Creating an Object from a JS Class


const john = new Person("John", 30);
john.greeting(); // Output: "Hello, my name is John and I am 30 years old."

The above code creates an object called "john" from the "Person" class and initializes its properties using the constructor method. The object can then call the "greeting" method to output a message.

Extending a JS Class

Inheritance is another important aspect of object-oriented programming. It allows you to create a new class based on an existing class. The new class inherits the properties and methods of the existing class and can also define its own properties and methods.

In JavaScript, a class can be extended using the "extends" keyword. The new class is defined using the "class" keyword and its properties and methods are defined inside the class block.

Example:


class Employee extends Person {
  constructor(name, age, position) {
    super(name, age);
    this.position = position;
  }
  
  getInfo() {
    console.log(`Name: ${this.name}, Age: ${this.age}, Position: ${this.position}`);
  }
}

const alice = new Employee("Alice", 25, "Manager");
alice.greeting(); // Output: "Hello, my name is Alice and I am 25 years old."
alice.getInfo(); // Output: "Name: Alice, Age: 25, Position: Manager"

The above code defines a new class called "Employee" that extends the "Person" class. It adds a new property called "position" and a new method called "getInfo". The "super" keyword is used to call the constructor method of the parent class.

An object of the "Employee" class can call both the methods of the "Person" class and the methods of the "Employee" class.

Conclusion

Classes in JavaScript provide a way to organize and reuse code. They make it easier to create complex applications by breaking them down into smaller, more manageable pieces. By using inheritance, you can create new classes based on existing ones and avoid duplicating code. With classes, you can create objects that have properties and methods that behave like real-world objects.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe