There are several ways to create a new instance of a JavaScript object:
- Using object literals:
let obj = {};
- Using the
Object
constructor:
let obj = new Object();
- Using
Object.create()
method:
let obj = Object.create(Object.prototype);
- 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.
+ There are no comments
Add yours