Skip to content
the poor coder

js class

Poor Man
Published
Reading time
2 min read
On this page

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#

Text

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#

Text

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:

Text

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.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC