Creating an object in JavaScript

There are no best way to create an object in javascript. But following are the few good practices. Depends on situation we have to choose the implementation.

function Person() {
    this.id = null;
    this.firstName = null;
    this.lastName = null;
    // more fields
    function fullName = function() {
        return this.firstName + ', ' + this.lastName; 
    };
}

Creating new object:

var person = new Person();

Using function constructor:

function Person(person) {
    if(person) {
        this.id = person.id;
        this.firstName = person.firstName;
        this.lastName = person.lastName;
    }
    else {
        this.id = null;
        this.firstName = null;
        this.lastName = null;
    }
}

Creating new object:

var person = new Person(person);

Singleton Javascript object:

function Person() {
    this.id = 1;
    this.firstName = "Ripon";
    this.lastName = "Datta";
    // more fields
};
var SingletonPersonClass = (function(){
    function SingletonClass() {
        var object = new Person();
        return object;
    }

    var personInstance;

    return {
        getSingletonInstance: function(){
            if (personInstance == null) {
                personInstance = new SingletonClass();
            }
            return personInstance;
        }
   };
})();

var person1 = SingletonPersonClass.getSingletonInstance();
var person2 = SingletonPersonClass.getSingletonInstance();
if (person1 == person2) {
    alert ("Singleton person object");
} else {
    alert ("Not Singleton");
}

Source code: How to create Singleton Javascript Object

Email
Print