From: David Mark on
On Jul 31, 12:58 pm, John G Harris <j...(a)nospam.demon.co.uk> wrote:
> On Thu, 29 Jul 2010 at 17:46:07, in comp.lang.javascript, David Mark
> wrote:>On Jul 29, 8:39 pm, Jesse <imje...(a)gmail.com> wrote:
> >> Hey, I've got a question about self invoking functions in javascript.
>
> >> What I'm doing is something similar to the following
>
> >> myNamespace =  {}; //namespace for holding any objects/functions
>
> >It's just an object and your code would be clearer without the
> >comment.
>
>   <snip>
>
> It's an object used to implement a namespace (by hand, as is common in
> javascript).

Yes.

>
> There *should* be a short comment. It should say briefly what this
> global variable is for, to be read by maintenance programmers in two
> year's time.

The comment seems superfluous to me, particularly due to the name of
the variable, which seems to be self-documenting enough.

>
> What's wrong is the name. When Jesse's code and Jim's code is merged
> next year then Jesse's myNameSpace and Jim's myNameSpace will clash
> horribly.


> Also it's far too long to be written many, many times.
>

The number of characters typed is never a concern (use an editor with
macros). And in the case of global "namespaces" they shouldn't need
to be written many times (if you do, you are likely writing slow code
that will minify poorly).
From: John G Harris on
On Sat, 31 Jul 2010 at 14:42:24, in comp.lang.javascript, David Mark
wrote:

<snip>
>The number of characters typed is never a concern (use an editor with
>macros).

No thank you. Some of us are allergic to numerous control codes. They're
for geeks who want to boast about remembering them.


>And in the case of global "namespaces" they shouldn't need
>to be written many times (if you do, you are likely writing slow code
>that will minify poorly).

So you reject the Math object, and would never write a javascript
application that uses lots of mathematical functions.

John
--
John Harris
From: David Mark on
On Aug 1, 12:41 pm, John G Harris <j...(a)nospam.demon.co.uk> wrote:
> On Sat, 31 Jul 2010 at 14:42:24, in comp.lang.javascript, David Mark
> wrote:
>
>   <snip>
>
> >The number of characters typed is never a concern (use an editor with
> >macros).
>
> No thank you. Some of us are allergic to numerous control codes. They're
> for geeks who want to boast about remembering them.

Who needs macros anyway? It was just an example. Many editors
provide auto-complete features. Furthermore, if you can remember Ctrl
+C and Ctrl+V you are golden. ;)

>
> >And in the case of global "namespaces" they shouldn't need
> >to be written many times (if you do, you are likely writing slow code
> >that will minify poorly).
>
> So you reject the Math object, and would never write a javascript
> application that uses lots of mathematical functions.
>

No, the built-in Math object is not a global "namespace".
From: Jesse on
On Aug 2, 11:00 am, David Mark <dmark.cins...(a)gmail.com> wrote:
> On Aug 1, 12:41 pm, John G Harris <j...(a)nospam.demon.co.uk> wrote:
>
> > On Sat, 31 Jul 2010 at 14:42:24, in comp.lang.javascript, David Mark
> > wrote:
>
> >   <snip>
>
> > >The number of characters typed is never a concern (use an editor with
> > >macros).
>
> > No thank you. Some of us are allergic to numerous control codes. They're
> > for geeks who want to boast about remembering them.
>
> Who needs macros anyway?  It was just an example.  Many editors
> provide auto-complete features.  Furthermore, if you can remember Ctrl
> +C and Ctrl+V you are golden.  ;)
>
>
>
> > >And in the case of global "namespaces" they shouldn't need
> > >to be written many times (if you do, you are likely writing slow code
> > >that will minify poorly).
>
> > So you reject the Math object, and would never write a javascript
> > application that uses lots of mathematical functions.
>
> No, the built-in Math object is not a global "namespace".

Thanks for the comments and criticisms :)

Unfortunately I didn't study computer science while at university and
have just picked up programming over the years. So I just wanted to
see if there was something wrong with using that pattern, or is it a
construct? ( these are the sorts of things I now wish I'd learnt while
at Uni)

I wanted to know if it was bad performance-wise, confusing for other's
reading it, or some other implications

>It's just an object and your code would be clearer without the
>comment.

The comments were the for example's sake

>The comment seems superfluous to me, particularly due to the name of
>the variable, which seems to be self-documenting enough.

As with the comments this was only named myNamespace for the example.
We use the name of the company which is pretty unique so very unlikely
to clash with anyone else's namespace
From: David Mark on
On Aug 1, 7:00 pm, David Mark <dmark.cins...(a)gmail.com> wrote:
> On Aug 1, 12:41 pm, John G Harris <j...(a)nospam.demon.co.uk> wrote:
>
> > On Sat, 31 Jul 2010 at 14:42:24, in comp.lang.javascript, David Mark
> > wrote:
>
> >   <snip>
>
> > >The number of characters typed is never a concern (use an editor with
> > >macros).
>
> > No thank you. Some of us are allergic to numerous control codes. They're
> > for geeks who want to boast about remembering them.
>
> Who needs macros anyway?  It was just an example.  Many editors
> provide auto-complete features.  Furthermore, if you can remember Ctrl
> +C and Ctrl+V you are golden.  ;)
>

And as for globals, you could peck away for hours using "A" in lieu of
whatever is deemed too typing intensive and then do a global replace
on "A.".

Typing should never be a consideration when naming variables or
properties. And typing the name of a global "namespace" over and over
indicates that inefficiencies are piling up (both in terms of
performance and size).

Math was mentioned as a counter-example (though it is not a global
variable). I suppose you could create local references to its methods
as well, but I'd have to check the specs to be sure that indirect
calls are allowed for all of them.