How to Create a New Instance of a JavaScript Object?

Estimated read time 1 min read

There are several ways to create a new instance of a JavaScript object:

  1. Using object literals:
let obj = {};
  1. Using the Object constructor:
let obj = new Object();
  1. Using Object.create() method:
let obj = Object.create(Object.prototype);
  1. Using a custom constructor function:
function MyObject(param1, param2) {
  this.param1 = param1;
  this.param2 = param2;

let obj = new MyObject("value1", "value2");

Note: The last method is useful when you want to create multiple instances of the same object type and set their initial state using parameters.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply