1

In javascript, what is the difference between:

var singleton = function(){ ... }

and

var singleton = new function(){ ... }

?

Declaring priviliged functions as described by crockford (http://www.crockford.com/javascript/private.html) only works using the latter.

jd.
  • 3,987
  • 7
  • 33
  • 45
  • I'm confused because the body of your question doesn't match what's asked in the title. That's a bit unfortunate as I really would like to know what happens if you call new on a singleton. Mainly, what happens when you do something like this: var singleton = new (function() { return function() {}; })(); Is the new applied to that returned function? – Bob Feb 12 '10 at 06:03
  • I guess we don't mean the same thing by 'singleton'. As for your question, look at CMS' answer below: " 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." – jd. Feb 12 '10 at 19:55
  • A singleton is a single instance of an object, it doesn't need the new operator. At least, that's what some people use the term for when referring to JavaScript. If you hold a different definition that's cool. As for that snippet of code I posted: I tried it and it failed. Since wrapping the function in parenthesis and appending () it executes the function and returns an object immediately, so the new doesn't work on an object. – Bob Feb 12 '10 at 22:50

2 Answers2

5

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
    }
  };
})();
Community
  • 1
  • 1
Christian C. Salvadó
  • 723,813
  • 173
  • 899
  • 828
0

I think that a privileged function as described by crockford would look like this:

function Test() {
     this.privileged = function() {
          alert('apple');
     }
     function anonymous() {
         alert('hello');
     }
     anonymous();
}

t = new Test; // hello
t.privileged(); // apple

// unlike the anonymous function, the privileged function can be accessed and
// modified after its declaration

t.privileged = function() {
     alert('orange');
}

t.privileged(); // orange
mattwindwer
  • 931
  • 9
  • 18