JavaScript function creates namespace
<p>Below there is the script which create namespace for module, I can't understand how it works after <code>parent = parent[parts[i]]</code>, how does it create nesting? Any suggestions?</p>

<pre><code>var MYAPP = MYAPP || {};
MYAPP.namespace = function (ns_string) {
    var parts = ns_string.split('.'),
        parent = MYAPP,
        i;

    if (parts[0] === "MYAPP") {
        parts = parts.slice(1);
    }
    for (i = 0; i &lt; parts.length; i += 1) {
        // create property if doesn't exist
        if (typeof parent[parts[i]] === "undefined") {
            parent[parts[i]] = {};
        }
        parent = parent[parts[i]];
    }
    return parent;
};

var module2 = MYAPP.namespace('MYAPP.modules.module2');
module2 === MYAPP.modules.module2; // true
</code></pre>
