Javascript: importing a namespace Flaagan style
In Flanagan’s ‘Javascript, The Definitive Guide’ section 10.2 suggets this
approach for importing symbols from a namespace:
<script type=“text/javascript”>
var com = {};
com.codeforpeople = {};
com.codeforpeople.www = {};
com.codeforpeople.www.a = function(){ alert(’http://www.codeforpeople.com’); }
com.codeforpeople.www.b = function(){ alert(’http://drawohara.com’); }
</script>
<script type=“text/javascript”>
var a = com.codeforpeople.www.a;
var b = com.codeforpeople.www.b;
a();
b();
</script>
var com = {};
com.codeforpeople = {};
com.codeforpeople.www = {};
com.codeforpeople.www.a = function(){ alert(’http://www.codeforpeople.com’); }
com.codeforpeople.www.b = function(){ alert(’http://drawohara.com’); }
</script>
<script type=“text/javascript”>
var a = com.codeforpeople.www.a;
var b = com.codeforpeople.www.b;
a();
b();
</script>
But I wonder, why on earth would you import symbols invidually when you could
simply shortcut the namespace and use that handle:
<script type=“text/javascript”>
var namespace = com.codeforpeople.www;
namespace.a();
namespace.b();
</script>
var namespace = com.codeforpeople.www;
namespace.a();
namespace.b();
</script>
Seems a lot more practical for a library with 4242 functions in it. Not to
mention it will work with namespaced primitives, etc. Of course you may be
into RSI… ;-)
David and I had this exchange, which sums it up i think:
From: “ara.t.howard” <ara.t.howard@gmail.com>
Date: July 31, 2007 11:45:29 AM MDT
To: David Flanagan <david@davidflanagan.com>
Subject: Re: Re:
On Jul 31, 2007, at 11:32 AM, David Flanagan wrote:
> tumblr.com doesn’t let me comment directly, does it?
nope. i just happen to be reading this javascript book and am
planning on hanging a cgi off of another server and writing a pure
javascript comment system for working with them ;-)
>
> The alternative you suggest works fine, but it isn’t actually
> importing any symbols. It is just defining a shorter name for the
> module’s original namespace.
>
right. i use the same technique in ruby quite often
md5 = Diget::MD5
md5.hexdigest ‘foobar’
> I think that many modules work best when the important symbols are
> imported into the global namespace. But if you do that, you can’t
> import everything, or you’ll get namespace pollution.
right. aliasing the namespace is an important alternative to pulling
symbols in one by one though - imho.
i’m really enjoying your book btw.
kind regards.
ps. i’ll append this response to the original post unless you object?
a @ http://drawohara.com/
—
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama
Date: July 31, 2007 11:45:29 AM MDT
To: David Flanagan <david@davidflanagan.com>
Subject: Re: Re:
On Jul 31, 2007, at 11:32 AM, David Flanagan wrote:
> tumblr.com doesn’t let me comment directly, does it?
nope. i just happen to be reading this javascript book and am
planning on hanging a cgi off of another server and writing a pure
javascript comment system for working with them ;-)
>
> The alternative you suggest works fine, but it isn’t actually
> importing any symbols. It is just defining a shorter name for the
> module’s original namespace.
>
right. i use the same technique in ruby quite often
md5 = Diget::MD5
md5.hexdigest ‘foobar’
> I think that many modules work best when the important symbols are
> imported into the global namespace. But if you do that, you can’t
> import everything, or you’ll get namespace pollution.
right. aliasing the namespace is an important alternative to pulling
symbols in one by one though - imho.
i’m really enjoying your book btw.
kind regards.
ps. i’ll append this response to the original post unless you object?
a @ http://drawohara.com/
—
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama