How can you programmatically add a function to a nested namespace?
<p>Say i have this function that dynamically creates my namespace for me when I just pass it a string, (I'm pretty sure basically what YUI JS library does):</p>

<pre><code>MyObj.namespace('fn.method.name');
</code></pre>

<p>would result in </p>

<pre><code>MyObj.fn.method.name = {}
</code></pre>

<p>being created - all three levels being equivalent to an empty object.</p>

<p>Now, what I want to do, though, is make the last level, in this case <code>name</code>, set to a function, but without having to redeclare the newly created object.<br>
So instead of doing this:</p>

<pre><code>function fnName() { /* some code here */ }
MyObj.namespace('fn.method.name');
MyObj.fn.method.name = new fnName();
</code></pre>

<p>i want to call something like:</p>

<pre><code>MyObj.add('fn.method.name', fnName);
</code></pre>

<p>And internally, the <code>add</code> method would programmatically instantiate the passed in function:</p>

<pre><code>MyObj.fn.method.name = new fnName()
</code></pre>

<p>In the way I have it implemented, I can create the namespace object and set it to an empty object, however, when I try to instantiate a passed in function and associate that namespace with the passed in function, it never gets added to the namespace.  Instead, an empty object is always returned.  Any ideas?</p>

<p>edit: Here is the <code>namespace</code> method. this is attached to the base object as a JSON object, so please ignore the formatting:</p>

<pre><code>namespace: function (ns) {
    var _ns = ns.split('.'), 
        i = 0, nsLen = _ns.length,
        root = this;
    if (_ns[0] === gNS) {
        _ns.shift();
            nsLen = _ns.length;
        }
        for (i = 0; i &lt; nsLen; i++) { 
            // create a property if it doesn't exist 
            var newNs = _ns[i];
            if (typeof root[newNs] === "undefined") {
                root[newNs] = {};
            }
            root = root[newNs];
        }
        return root;
    }
</code></pre>

<p>edit2 - removed the passed in <code>fn</code> argument</p>
