Object-Oriented JavaScript — Aggregation, and Inheritance

Object-Oriented JavaScript — Aggregation, and Inheritance

“Favor object composition over class inheritance. ... class inheritance is the tightest form of coupling available in object-oriented design, ... Aggregation When an object is formed from an enumerable collection of subobjects.

JavaScript is partly an object-oriented language.

To learn JavaScript, we got to learn the object-oriented parts of JavaScript.

In this article, we’ll go through the object-oriented parts of JavaScript.

Aggregation

The ability to combine several objects into a new one is known as aggregation or composition.

Multiple small objects are easier to manage than one large object.

There are several ways to do with JavaScript.

We can use the spread operator or Object.assign method to combine multiple objects into one.

To use the Object.assign method, we can write:

const obj1 = {
  foo: 1
}
const obj2 = {
  bar: 2
}
const obj3 = {
  bax: 3
}
const merged = Object.assign({}, obj1, obj2, obj3);

We have 3 objects, and we passed them all into Object.assign so we can merge them together and returns a new object with the properties of all of them.

Also, we can use the spread operator by writing:

const obj1 = {
  foo: 1
}
const obj2 = {
  bar: 2
}
const obj3 = {
  bax: 3
}
const merged = {
  ...obj1,
  ...obj2,
  ...obj3
};

This will also combine the 3 objects together.

Another way to aggregate objects is having child objects by having as properties of another object.

For instance, a Book object can have multiple Author objects, Publisher objects, Chapter objects, and so on.

Inheritance

Inheritance is an elegant way to let reuse existing code.

We can create objects and inherit from directly.

And we can create JavaScript classes with subclasses.

Classes and subclasses are syntactic sugar for parent and child constructor functions.

To create an object that uses another object as a prototype, we can use the Object.create method.

For instance, we can write:

const obj = {
  foo: 1
}

const child = Object.create(obj);

Then child inherits from the foo property from obj .

Most objects have the Object.prototype as their prototype if they don’t inherit from anything explicitly.

We can create subclasses with the extends keyword.

software-development programming technology javascript web-development

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Offshore Software Development - Best Practices

To make the most out of the benefits of offshore software development, you should understand the crucial factors that affect offshore development.

Software Developer vs Web Developer | Difference Web Developer & Software Developer

Software Developer vs Web Developer | Difference Web Developer & Software Developer | Software development and web development are normally used interchangeably. Although both include development, a software developer has different responsibilities from that of a web developer.

5 Core Criteria for Selecting Software Development Company - TopDevelopers.co

Check out these five criteria for the selection of your software vendor, and you will never regret having the wrong quality product made for you.

Offshore Software and Web Application Development Company- MobiWeb Technologies

Mobiweb Technologies is a prestigious IT company headquartered in Indore providing promising and cost effective software and web development services across the globe.

Offshore Software and Web Application Development Company- MobiWeb Technologies

Mobiweb Technologies is a prestigious IT company headquartered in Indore providing promising and cost effective software and web development services across the globe.