Don't forget:
var counter = (function () {
return {};
})();
Wrap the function in parens and it makes it clear that you're calling the
function and assigning the return value. This is quite a well-accepted
idiom, and means you don't need to scan to the bottom of the function.
On 16 December 2010 00:14, Garrett Smith <[email protected]> wrote:
> On 12/13/10, Lasse Reichstein <[email protected]> wrote:
> > On Mon, 13 Dec 2010 05:21:53 +0100, Rick Waldron <[email protected]
> >
> > wrote:
> >
> >> I agree with Garrett both in style and execution and I'd like to take
> his
> >> example one step further...
> >>
> >>
> >> var counter = new function() {
> >>
> >> var i = 0;
> >>
> >> this.next = function() {
> >> return ++i;
> >> };
> >>
> >> this.reset = function() {
> >> return (i = 0);
> >> };
> >> };
> >
> > Still seems like overkill to me, at least syntactically. How about just:
> > var counter = function() {
> > var i = 0;
> > return {
> > next : function () { return ++i; },
> > reset: function () { return i = 0; }
> > };
> > }();
> >
> Sure, that works just as well.
>
> Performance differences should not be expected.
>
> The difference for me is that very first line tells me that I am
> creating a new Object.
>
> var counter = new function() { // must be an object !
> //...
> }
>
> Where in contrast:
>
> var counter = function() { // `counter` is a function?
> // ...
>
> return { };
> }(); // No, the function is called immediately, returning {}.
>
> With newExpression singleton, is that it is clear an object is created
> on line 1.
>
> This must be true because the outer function is called (newed,
> actually) immediately, it means that `counter` must be the *result* of
> `new` on that function, not the function itself.
>
> In contrast, with the module pattern, it is not immediately clear that
> `counter` is not the function untilthe end of the outer function with
> `return { ... }` and then `}();`. The module pattern requires at least
> one more visual scan of the code.
>
> The downside to `new function(){ .. }` for me is when I have a lot of
> assigments, e.g. "this.x", "this.y", "this.z=". It gets worse when
> they are scattered. In that case, object literal because you can see
> where it starts and ends and doesn't require all the "this.".
>
> Either pattern can work just fine. In my experience at Yahoo, I saw
> the module pattern used very badly and was being advocated by one of
> the premier "experts" who could technically explain it, but did not
> seem to have much practical usage experience of the pattern, including
> how *not* to use it. I first misbelieved that the pattern itself was
> bad.
>
> Fortunately I saw past that; the module pattern is fine and so is
> newExpression. I can't see any reason for rejecting either with this
> toy example; both work fine.
>
> Garrett
>
> --
> To view archived discussions from the original JSMentors Mailman list:
> http://www.mail-archive.com/[email protected]/
>
> To search via a non-Google archive, visit here:
> http://www.mail-archive.com/[email protected]/
>
> To unsubscribe from this group, send email to
> [email protected]<jsmentors%[email protected]>
>
--
Nick Morgan
http://skilldrick.co.uk
@skilldrick <http://twitter.com/skilldrick>
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]