js oop

JavaScript Object-Oriented Programming (OOP)

JavaScript is a popular programming language used for building web applications. One of the main features of JavaScript is its ability to use object-oriented programming (OOP) concepts. OOP is a programming paradigm that focuses on creating objects that have properties and methods, which can interact with each other.

What is an Object in JavaScript?

In JavaScript, an object is a collection of key-value pairs. Each key is a string, and the value can be any data type, including other objects or functions. Objects can be created using two methods:

  • Literals: const myObject = {key1: value1, key2: value2};
  • Constructor functions: function MyClass() {this.key1 = value1; this.key2 = value2;}

What is a Class in JavaScript?

A class is a blueprint for creating objects. In JavaScript, classes can be created using the class keyword. They are essentially constructor functions with a more concise syntax:

class MyClass {
  constructor(key1, key2) {
    this.key1 = key1;
    this.key2 = key2;

What are Prototypes in JavaScript?

Prototypes are a way of sharing properties and methods between objects in JavaScript. Every object in JavaScript has a prototype, which is a reference to another object. When you access a property or method on an object, JavaScript first looks for it on the object itself. If it's not found, it looks for it on the object's prototype, and so on up the prototype chain until it reaches the top-level Object prototype.

MyClass.prototype.myMethod = function() {
  console.log('Hello World!');

What is Inheritance in JavaScript?

Inheritance is a way of creating new classes based on existing ones. In JavaScript, inheritance is implemented using prototypes. You can create a new class that inherits from an existing class by setting its prototype to an instance of the parent class:

class MySubclass extends MyClass {
  constructor(key1, key2, key3) {
    super(key1, key2);
    this.key3 = key3;

This creates a new class called MySubclass that inherits from MyClass. MySubclass has access to all of the properties and methods of MyClass, as well as any additional properties or methods defined in its own constructor.

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]