On Dec 21, 2010, at 2:02 PM, Thr4wn <[email protected]> wrote:
> So far I'm hearing that the "module" (or "closure") pattern can have
> slow performance, so don't use it as a default.
I love the closure pattern and think it's one of the reasons why JS is so
powerful.
>
> So if it's between the literal (or 'singleton' as I understand
> Crockford to call it) pattern and the namespace pattern, then which
> one should we use?
>
> The only meaningful difference I can see left is that the singleton
> pattern does not allow one method to use the other methods. So the
> following won't work...
>
> namespace = {
> a: function() {
> return "hello world";
> },
> b: function() {
> alert( a() );
> }
You can get at 'a' with alert(this.a()); a will be defined when you call the b
function. 'this' used within the context of a method on the object will point
to the object itself.
>
> because `a` hasn't been defined, but the following will work...
>
> namespace = {};
> namespace.a = function() {
> return "hello world";
> };
> namespace.b = function() {
> alert( namespace.a() );
> };
>
>
>
> So my conclusion is to use the namespace pattern because:
>
> 1) closure pattern can have slow performance
IMO this is a matter of preference, I wouldn't throw this pattern out because
it could have bad perf. Has anyone seen real world use cases where this
pattern has been slow? Articles, links?
> 2) singletons don't allow mutual usage or recursion.
>
>
>
>
> --
> 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]
--
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]