var obj = new function () { // open IIC var uid = computeUID(); this.first = 'Jane'; this.last = 'Doe'; this.uid = uid; }(); // close IICThe immediately invoked constructor provides you with a private scope, in which the variable uid lives. You can then use this to set up the instance that has been created by new. Additionally, new returns that instance for you. You can save a few characters by omitting the parentheses at the end:
var obj = new function () { // open IIC ... }; // close IICThe above code works, because parentheses are optional if you call a constructor with zero arguments. That is, the following two statements are equivalent.
var d = new Date(); var d = new Date;However, I prefer the first variant; I find it more explicit.
Obviously, an IIFE is always an alternative to an immediately invoked constructor. It has two advantages: The code should look more familiar and you can use an object literal.
var obj = function () { var uid = computeUID(); return { first: 'Jane', last: 'Doe', uid: uid }; }();
({ errorCount: 0, step1: function () { ... }, step2: function () { ... }, logError(message) { console.log(message); this.errorCount++; }, main: function () { this.step1(); this.step2(); console.log('Errors:'+this.errorCount); } }.main());Note that we can’t start the first line with a brace, because then the object literal would be interpreted as a code block by JavaScript [2].
Depending on your taste, you may prefer an IIFE:
(function () { var errorCount = 0; function step1() { ... } function step2() { ... } function logError(message) { console.log(message); errorCount++; } // main step1(); step2(); console.log('Errors:'+errorCount); }());Obviously, you could also turn the lines of main into a function and invoke that function last inside the IIFE.