The difference is mainly that in your second example, you are using the Function Expression as a Constructor, the new
operator will cause the function to be automatically executed, and the this
value inside that function will refer to a newly created object.
If you don't return anything (or you don't return a non-primitive value) from that function, the this
value will be returned and assigned to your singleton
variable.
Privileged methods can also be used in your second example, a common pattern is use an immediately invoked function expression, creating a closure where the private members are accessible, then you can return an object that contains your public API, e.g.:
var singleton = (function () {
var privateVar = 1;
function privateMethod () {/*...*/}
return { // public API
publicMethod: function () {
// private members are available here
}
};
})();