From: Scott Sauyet on
(I know I've seen a discussion of this here not too long ago, but
search is failing me. Any pointers to prior threads would be
appreciated.)

I'm pretty sure posters here had some reasonable critiques of using
this style to declare a singleton:

var singleton = new function() {
var privateProp1 = //...
var privateFn = function(...) {...}
this.publicProp1 = // calculated using privateProp1 and
privateFn1
this.publicProp2 = // calculated using privateProp1 and
privateFn1
};

Obviously I could do this instead:

var singleton = (function() {
var privateProp1 = //...
var privateFn = function(...) {...}
return {
publicProp1: // calculated using privateProp1 and privateFn1,
publicProp2: // calculated using privateProp1 and privateFn1
};
};

But the former syntax seems somewhat cleaner. Are there good reasons
not to use it?

Thanks for any feedback,

--
Scott
From: Scott Sauyet on
Scott Sauyetwrote:

Sorry, bad wrapping. This is what I meant:

  var singleton = new function() {
    var privateProp1 = //...
    var privateFn = function(...) {...}
    this.publicProp1 = // calc using privateProp1 and privateFn1;
    this.publicProp2 = // calc using privateProp1 and privateFn1;
  };

--
Scott
From: Garrett Smith on
Scott Sauyet wrote:
> (I know I've seen a discussion of this here not too long ago, but
> search is failing me. Any pointers to prior threads would be
> appreciated.)
>
> I'm pretty sure posters here had some reasonable critiques of using
> this style to declare a singleton:
>
> var singleton = new function() {
> var privateProp1 = //...
> var privateFn = function(...) {...}
> this.publicProp1 = // calculated using privateProp1 and
> privateFn1

<snip>

You want to know which is better: 1 or 2. Based on what was written, I
interpreted those as (1) creating a NewExpression and assigning
properties to `this` vs (2) calling a function that returns an object.
Your example #2 did not show the function being called but I assumed
that is what you meant.

//Example 1:
var singleton = new function() {
var privateProp = 1;
function privateFn( ) {
return privateProp + Math.random();
}
this.publicFn = privateFn;
};

//Example 2:
var singleton = (function(){
var privateProp1 = 1;
function privateFn() {
return privateProp1 + Math.random();
}
return {
publicFn : privateFn
};
})();

Did I get that right?

I find both easy to understand.

#1 has a longer prototype chain but is shorter to write. Line 1 has a
NewExpression, which shows that an object is being created and will be
assigned to identifier `singleton`.

Based on the context, I don't have much else to say.

When posting NG code, it is a good idea to make sure that it is
executable as transmitted and to format the lines to 72 characters wide
to avoid wrapping.
--
Garrett
comp.lang.javascript FAQ: http://jibbering.com/faq/
From: Scott Sauyet on
Garrett Smith wrote:
> Scott Sauyet wrote:
> You want to know which is better: 1 or 2. Based on what was written, I
> interpreted those as (1) creating a NewExpression and assigning
> properties to `this` vs (2) calling a function that returns an object.

Yes, that's what I intended.

> Your example #2 did not show the function being called but I assumed
> that is what you meant.
>
> //Example 1:
> var singleton = new function() {
>    var privateProp = 1;
>    function privateFn( ) {
>      return privateProp +  Math.random();
>    }
>    this.publicFn = privateFn;
>
> };
>
> //Example 2:
> var singleton = (function(){
>    var privateProp1 = 1;
>    function privateFn() {
>      return privateProp1 + Math.random();
>    }
>    return {
>      publicFn : privateFn
>    };
>
> })();
>
> Did I get that right?

Not quite. I should have added "())" before the final semicolon in
the second example, e.g.

var singleton = (function() {
var privateProp1 = //...
var privateFn = function(...) {...}
return {
publicProp1: // calculated using privateProp1 and privateFn1,
publicProp2: // calculated using privateProp1 and privateFn1
};
}());


I would not expect to make privateFn available publicly at all,
although it might be called by functions I do expose.

Perhaps a trivial-but-complete example would be in order. I could use
technique 1:

var idGen = new function() {
var rowPrefix = "row-", cellPrefix = "cell-", count=0;
var nextValue = function() {
return count++;
};
this.nextRow = function() {return rowPrefix + nextValue();};
this.nextCell = function() {return cellPrefix + nextValue();};
};
// idGen.nextRow() ==> "row-0"
// idGen.nextCell() ==> "cell-1"
// idGen.nextCell() ==> "cell-2"
// idGen.nextRow() ==> "row-3"


instead of my usual technique 2:

var idGen = (function() {
var rowPrefix = "row-", cellPrefix = "cell-", count=0;
var nextValue = function() {
return count++;
};
return {
nextRow: function() {return rowPrefix + nextValue();},
nextCell: function() {return cellPrefix + nextValue();},
}
}());
// idGen.nextRow() ==> "row-0"
// idGen.nextCell() ==> "cell-1"
// idGen.nextCell() ==> "cell-2"
// idGen.nextRow() ==> "row-3"


I do recognize that neither of these is necessarily the best way to
write these little functions. I'm just curious as to the benefits of
one technique over the other or if there are potential issues with
either.

> I find both easy to understand.
>
> #1 has a longer prototype chain but is shorter to write. Line 1 has a
> NewExpression, which shows that an object is being created and will be
> assigned to identifier `singleton`.
>
> Based on the context, I don't have much else to say.

I've generally used technique 2. Today I was writing some code and
just started it out of the blue with technique 1. I'm not sure why.
It seems a little cleaner, a little clearer, though, and I really
wonder if there is some compelling reason not to use it.


> When posting NG code, it is a good idea to make sure that it is
> executable as transmitted and to format the lines to 72 characters wide
> to avoid wrapping.

Yes it is. I apologize.

--
Scott
From: Eric Bednarz on
Scott Sauyet <scott.sauyet(a)gmail.com> writes:

> I'm pretty sure posters here had some reasonable critiques of using
> this style to declare a singleton:

I'm pretty sure I would like to know how and more than all why one would
try to implement the singleton pattern in a class-free language.