Building a Dynamic or Adding to a Javascript Namespace

Here is a quick example at how to build to an existing javascript name space. It is also used at building dynamic namespaces.

<script type="text/javascript">
 
  //root namespace
  var myprogram = {
 
    //define the children namespaces
 
    init : function() {
      //onloads here
     alert('myprogram.init() was called');
    }
  }
 
  //adding to the myprogram namespace
  myprogram['test'] = function() { alert('Test'); };
 
</script>

The above creates a root namespace "myprogram". After its creation, we add the child namespace function "test". One could call the child namespace by simply adding it to a link for example.

<a href="#" onclick="myprogram.test();">Click Here to alert "Test"</a>