1

It looks like there a number of ways to create object in Javascript per below:

var person = {
    firstName:"John",
    lastName:"Doe",
    age:50,
    eyeColor:"blue"
};

function person(){
    this.firstName = 'first';
    this.lastName = 'last';
    this.age = 'age';
    this.eyeColor = 'eyecolor';
}

person = (function () {
    this.firstName = 'first';
        this.lastName = 'last';
        this.age = 'age';
        this.eyeColor = 'eyecolor';
}();

Could someone please help me understand which one of above is the best practice to use?

And also on what scenarios do we use self-invoking function?

royhowie
  • 11,075
  • 14
  • 50
  • 67
Nil Pun
  • 17,035
  • 39
  • 172
  • 294
  • 5
    It depends on what you want to do with those objects later, so there's really no best way of doing it. – Ja͢ck Feb 11 '15 at 05:29
  • possible duplicate of [Which way is best for creating an object in javascript? is "var" necessary before variable of object?](http://stackoverflow.com/questions/6843951/which-way-is-best-for-creating-an-object-in-javascript-is-var-necessary-befor) – V31 Feb 11 '15 at 05:45

2 Answers2

2

If you want to get the most bang for your buck, you can use a function, which allows you to set "public" and "private" variables. For example:

function Person (name, age) {
    var number = (Math.random() * 100) | 0;
    // number is private; there's no way to access it outside
    // of the scope of this function
    var surpriseParty = new Date(Date.now() + number*24*3600*1000);
    // we're throwing you a party within the next 100 days
    this.name = name;    // public
    this.age = age;      // public
    return {
        getName: function () { return name },
        getAge: function () { return name },
        getPartyDate: function () { return surpriseParty }
    }
}

As commented in the code above, now only certain variables can be accessed:

var p = new Person("royhowie", 18);
// p.getName() => "royhowie"
// p.name => "royhowie"
// p.getAge() => 18
// p.age => 18
// p.number => undefined
// p.surpriseParty => undefined
// p.getPartyDate() => date within the next 100 days

In truth, though, you should use whatever fits the bill best. The above method is an easy way to encapsulate data, but if you want everything to be public, use the normal object syntax:

var p = { name: "royhowie", age: 18, surpriseParty: new Date() };

I wouldn't recommend the self-executing anonymous function syntax. You should declare the function elsewhere (at least for debugging); this at least gives you the option of creating two different instances of that object.

royhowie
  • 11,075
  • 14
  • 50
  • 67
0

This question is already answered here which correctly states that there is no recommended way of creating a JavaScript Object. It completely depends upon your use case.

According to W3schools there is a standard way of creating objects:

The standard way to create an "object type" is to use an object constructor function. Here is an example:

function person(first, last, age, eyecolor) {
    this.firstName = first;
    this.lastName = last;
    this.age = age;
    this.eyeColor = eyecolor;
}
var myFather = new person("John", "Doe", 50, "blue");
var myMother = new person("Sally", "Rally", 48, "green");

Hope this helps! :)

Community
  • 1
  • 1
Fahim Hossain
  • 1,671
  • 13
  • 16
  • 1
    Please **do not** cite W3Schools. Read [this page](http://www.w3fools.com). There are [so many](http://www.webplatform.org) other [references](https://developer.mozilla.org/en-US/) available that are 100x better. – royhowie Feb 11 '15 at 05:45