Different ways of creating an object in javascript

Different ways of creating an object in javascript

ยท

4 min read

Welcome to my first tech blog! ๐Ÿ˜Ž

In this one, I am going to tell you everything you need to know about constructor functions, factory functions, and classes in JavaScript.

So let's get started ๐Ÿš€

Imagine you are a pastry chef, and you need to create different types of cakes. You have three options to do this - using a cake mold, a cookie cutter, or a cake recipe.

Constructor Functions

A constructor function is like a cake mold. It's a template that you can use to create new objects with the same properties and methods. For example, you can create a Cake constructor function that takes in parameters for the flavor and size of the cake and returns a new cake object with those properties. You can then use this constructor function to create multiple cakes with the same properties and methods.

function Cake(flavor, size) {
  this.flavor = flavor;
  this.size = size;
  this.bake = function() {
    console.log(`Baking a ${this.size} ${this.flavor} cake!`);
  }
}

const vanillaCake = new Cake('vanilla', 'medium');
vanillaCake.bake(); // Baking a medium vanilla cake!

In this example, the Cake constructor function takes in parameters for the flavor and size of the cake and returns a new cake object with those properties. You can then use the new keyword to create a new cake object and call its bake() method.

Factory Functions

A factory function is like a cookie cutter. It's a function that returns a new object without using the new keyword. For example, you can create a CakeFactory function that takes in a flavor and size and returns a new cake object with those properties.

function CakeFactory(flavor, size) {
  const cake = {};
  cake.flavor = flavor;
  cake.size = size;
  cake.bake = function() {
    console.log(`Baking a ${this.size} ${this.flavor} cake!`);
  }
  return cake;
}

const chocolateCake = CakeFactory('chocolate', 'large');
chocolateCake.bake(); // Baking a large chocolate cake!

In this example, the CakeFactory function takes in parameters for the flavor and size of the cake and returns a new cake object with those properties. You can then call the bake() method on the cake object.

Classes

A class is like a cake recipe. It's a blueprint that you can use to create new objects. For example, you can create a Cake class with properties and methods for the flavor and size of the cake.

Here's an example of a Cake class:

class Cake {
  constructor(flavor, size) {
    this.flavor = flavor;
    this.size = size;
  }

  bake() {
    console.log(`Baking a ${this.size} ${this.flavor} cake!`);
  }
}

const strawberryCake = new Cake('strawberry', 'small');
strawberryCake.bake(); // Baking a small strawberry cake!

In this example, the Cake class has a constructor method that takes in parameters for the flavor and size of the cake and sets those properties on the cake object. It also has a bake() method that you can call on the cake object.

Summary

Constructor functions are like a recipe for making objects. You start by creating a blueprint for what you want the object to look like, and then you use the new keyword to make a copy of that object. It's like following a recipe to make a cake - you start with a set of ingredients and instructions, and then you use those to create a delicious dessert.

Factory functions, on the other hand, are like a magical kitchen gadget that instantly creates new objects. You don't need to use the new keyword or create a blueprint beforehand - you simply call the function and it creates a new object for you. It's like using a 3D printer to make a new cake - you just press a button and it out pops a fresh dessert.

Classes are like a fancy bakery where you can order customized cakes. You start by creating a blueprint for what you want the cake to look like, and then you use that blueprint to make as many cakes as you want. It's like going to a cake shop and ordering a personalized dessert - the bakery already has a set of recipes and designs, and they can use those to make a cake that's tailored to your preferences.

So, whether you're following a recipe, using a kitchen gadget, or ordering from a fancy bakery, there are many ways to create objects in JavaScript. The key is to choose the method that's best suited for your needs - whether that's a constructor function, a factory function, or a class.


That's it, I hope you're not craving desserts like I am. I'm gonna go now and treat myself a tasty chocolate truffle pastry ๐Ÿ˜‹

Thanks for the read ๐Ÿ˜ƒ

ย