Skip to content
the poor coder

js oop

Poor Man
Published
Reading time
2 min read
On this page

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:

Text

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.

Text

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:

Text

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.

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