O'Reilly logo
  • Patrick S thinks this is interesting:

var user = {
  first: 'Brad',
  last: 'Dayley',
  getName: function( ) { return this.first + " " + this.last; }};

These first two options work very well for simple objects that you do not need to reuse later. A better method for reusable objects is to actually enclose the object inside its own function block. This has the advantage of allowing you to keep all of the code pertaining to the object local to the object itself. For example:

function User(first, last){
  this.first = first;
  this.last = last;
  this.getName = function( ) { return this.first + " " + this.last; }};};
var user = new User("Brad", "...

From

Cover of Node.js, MongoDB, and AngularJS Web Development

Note

Object creation - using var or function