Create JavaScript Object

By Object() constructor:

var obj = new Object();

By Object.create() method: Object.create: Will return a new object with properties of prototypeObject.

var obj = Object.create(prototypeObject);

By brackets: It is the same as Object.create() method with a null parameter.

var obj = {};

By function constructor:

var MySite = function(website) {
  this.website = website;
}
var obj = new MySite("w3spoint.com"); 

By function constructor with prototype:

function MySite(){};
MySite.prototype.name = "w3spoint.com";
var obj = new MySite();

By ES6 class syntax:

class MySite {
  constructor(name) {
    this.name = name;
  }
}
var obj = new MySite("w3spoint.com");

By Singleton pattern:

var obj = new function(){
  this.name = "w3spoint.com";
}

Related topics:

Please follow and like us:
Content Protection by DMCA.com