Skip to content

Latest commit

 

History

History
61 lines (43 loc) · 986 Bytes

File metadata and controls

61 lines (43 loc) · 986 Bytes

Question

What are the possible ways to create an Object in Javascript?

YouTube Video

youtube thumbnail

Answer

  1. Simple JSON Object
const shopCart = {
  items: 0,
  list: [],
};
  1. Using Object constructor
const shopCart = new Object();
  1. Using Function
const ShopCartClass = function () {
  this.items = 0;
  this.list = [];
};

const cart = new ShopCartClass();
  1. Using Class
class ShopCartClass {
  this.items = 0;
  this.list = [];
};

const cart = new ShopCartClass();
  1. Using Object.create(null) to inherit from the null object and remove all prototype methods
const cart = Object.create(null);
  1. Using IIFE (Immediately Invoked Function Expression)
const cart = new (function () {
  this.items = 0;
  this.list = [];
})();