On Apr 22, 9:57 am, disccomp <discc...@gmail.com> wrote:
> Here's the working code:http://pastie.org/454045

OK, here it is:

| $N = function(ns){

*Always* declare variables.  I can't see why you don't use:

  function $N(ns) {

or, because I dislike the use of $ in variable names:

  function getNamespace(ns) {

|   /* First split the namespace string separating each level of the
namespace object.*/
|   var splitNs = ns.split(".");

Ensure you have a string before trying to call one of its methods,
otherwise you'll get an error:

  var splitNs = String(ns).split('.');

|   /* Define a string, which will hold the name of the object we are
currently working with. Initialize to the first part.*/
|   var builtNs = splitNs[0];

builtNS might be undefined, a moot point since it isn't used.

|   var i, base = window;

Do not augment host objects, use the global object.  You can get a
reference to the global object using:

  var base = (function(){ return this;})();

|   for (i = 0; i < splitNs.length; i++){
|     if (typeof(base[ splitNs[i] ]) == "undefined") base[ splitNs
[i] ] = {};
|     base = base[ splitNs[i] ];
|   }

A couple of points:

 1. Store a refernce to splitNs[i] so you don't have to keep getting

 2. typeof is not a function so the brackets around the argument
aren't required:

    var nsPart;
    for (var i=0, len=splitNs.length; i<len; i++){
      nsPart = splitNs[i];
        if (typeof base[nsPart] == 'undefined') {
          base[nsPart] = {};
      base = base[nsPart];

 3. Browsers add host objects as properties of the global object, when
tested with typeof they can return any value they want, even
"undefined" when they aren't, so testing explicitly for "undefined" is
not particularly safe where host objects are concerned (e.g. in IE,
the typeof operator returns "unknown" for ActiveX objects).

4. At this point, base should be tested to see if it's an object:

    if (typeof base[nsPart] == 'object') {
      base = base[naPart];
    } else {
      ... what now?

We now are in a dilema.  Say were were given:


and we find that foo.bar is not undefined, but not an object either -
what now?  What if the parts of the namespace are not valid ECMA
identifiers?  Should they be checked?  And so on.

|   return base; // Return the namespace as an object.
| }

Whether base is an ojbect or not wasn't tested (originally).You don't
know base is an object, all you know is that you think it's not
undefined.  It could be anything that reutrns false to the typeof
test, perhaps a number, string or boolean.

Using a "namespace" is no better than using any particular string of
characters for a variable name, e.g.

  var foo = {
    bar = function() {...},

is no safer from collision than:

  var foo_bar = function() {...}

Further, you don't know if the object returned from $N is the one you
really want, or if it's some other object created by something you
don't know about.  Providing a namespace function infers that it some
safety is provided from collision, it does nothing that:

  var foo = foo || {};

doesn't do in a single line, with all the same pitfalls.  At least the
above statement has no pretence otherwise and it's a lot less code.

Before creating functions, it is a good idea to specify exactly what
they should do and investigate various options and risks.  You may
find from time to time that they offer nothing useful.

You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
For more options, visit this group at 

Reply via email to