Aug 2, 2019· 3 mins to read

Change the way you create an Object - Javascript Weekly


Change the way you create an Object - Javascript Weekly

In this article, we will see different ways to create an object in javascript. Change the way you create an Object - Javascript Weekly

Promise inside a loop - javascript weekly

Prototypal Inheritance - Javascript weekly

Understanding Closures in Javascript

Currying in Javascript

Object plays an important role in javascript ecosystem. we as a javascript developers creates a lots of objects while writing code. we will see some different way to do that

Object as literal

this is a default way to create an object. creating an object with curly braces is the popular way in Object Creation

const User = {
  name: "John",
  age: 25,
};

object as literal allows to add the property to an object dynamically. For Example, if you want add a property to User object, you can do it like

User.address = "Country";

In addition to that, you can also have a nested property inside the object literal

const Company = {
  name: "Test",
  tax: {
    id: "12345",
  },
};

“new” Operator

you can also create an object with key word new.

function Document(name, category) {
  this.name = name;
  this.category = category;
}

let salarydocument = new Document("salary", "SALARY");

let insurance = new Document("insurance", "INSURANCE");

main advantage of using new operator is, you can use prototype to chain methods. Prototype Chain is an important concept in using new keyword

function Language(name, shortform) {
  this.name = name;
  this.shortform = shortform;
}

Language.prototype.getShortForm = function () {
  return this.shortform;
};

let english = new Language("english", "en");

Object.create() method

Object create method allows you to create an object from an existing one.

For Example, Let’s say that you have an object Message, to create an Object with the same property as Message. you can do something like

let Message = {
  title: "Hola!",
  body: "Welcome",
};

let greeting = Object.create(Message);

console.log(greeting.title);

you can also add property to greeting object

let Message = {
  title: "Hola!",
  body: "Welcome",
};

let greeting = Object.create(Message, {
  type: {
    writable: true,
    enumerable: true,
    configurable: false,
    value: "Data",
  },
});

console.log(greeting.type);

Using ES6 Class

ES6 class is an another way to create objects in javascript

class User {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }

  getInfo() {
    console.log(`Hey ${this.name} Age : ${this.age}`);
  }
}

let admin = new User("admin", 40);

admin.getInfo();

these are all the different ways to create an Object in javascript.

Change the way you create an Object - Javascript Weekly

we will see more aboout Javascript in upcoming articles. I am a big fan of Eric Elliot’s work on Javascript Stuffs. checkout his latest article.

Do React Hooks Replace Higher Order Components (HOCs)?

Until then Happy Coding :-)

Copyright © Cloudnweb. All rights reserved.