How to create an object in Javascript?
There are various ways of creating an Object in JavaScript.
- With the Object():
Example :var name = new Object();
- With Object.create():
Example :var name = Object.create(null);
- With the bracket syntactig sugar:
Example :var name = {};
- With a function constructor:
Example :
var ObjectName = function(Vname) {
this. Vname = Vname
}
var NewVar = new ObjectName("BestInterviewQuestion.com"); - With function constructor + prototype:
Example :
function myObj(){};
myObj.prototype.name = "bestinterviewquestion.com";
var Newk = new myObj(); - With ES6 class:
Example :
class myObject {
constructor(name) {
this.name = name;
}
}
var e = new myObject("bestinterviewquestion.com");
BY Best Interview Question ON 18 Mar 2020