CodesJava

Easy learning with example program codes

Create JavaScript object


By Object() constructor:

var obj = new Object();

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

var obj = Object.create(prototypeObject);

By bracket’s:
It is same as Object.create() method with null parameter.

var obj = {};

By function constructor:

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

By function constructor with prototype:

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

By ES6 class syntax:

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

By Singleton pattern:

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

Related topics:

Please follow and like us:


Copyright © 2019 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki