On Dec 22, 6:02 am, 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.
Only avoid it if performance is an issue and you've identified the
module pattern as being significant in comparison to other processing.
> 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() );
> }
> }
>
> because `a` hasn't been defined, but the following will work...
a has been defined but isn't on b's scope chain (well, not the a you
want anyway) but you could use:
b: function() {
alert(namespace.a());
}
or (provided b is called as a method of namespace):
b: function() {
alert(this.a());
}
Incidentally, that "pattern" is called an object literal.
> namespace = {};
> namespace.a = function() {
> return "hello world";};
>
> namespace.b = function() {
> alert( namespace.a() );
> };
Which is just another way of writing the object literal above, the
outcome is identical.
> So my conclusion is to use the namespace pattern because:
>
> 1) closure pattern can have slow performance
Moot.
> 2) singletons don't allow mutual usage or recursion.
Whatever mutual usage is, and it has nothing to do with recursion as I
understand it. Above you've created exactly the same object using two
slightly different approaches. It's the same as giving the following
two different names:
var a = ['a','b'];
and
var a = [];
a[0] = 'a';
a[1] = 'b';
The first is an array literal, the second I'd call "idividual
assignment of array values". It's needlessly longer and has a suitably
long winded name.
--
Rob
--
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]