Saturday, October 20, 2012

Javascript Namespace

main.js
$(function(){
   var o = new ProjectNS.Order(1, "A Customer");
   alert(o.id);
   alert(o.customer.name);
});

order.js
(function(ns){
   ns.Order = function(id, custName){
      this.id = id,
      this.customer = new ns.Customer(custName)
   };
}(window.ProjectNS = window.ProjectNS || {}));

Customer.js
(function(ns){
   ns.Customer = function(name){
      this.name = name
   };
}(window.ProjectNS = window.ProjectNS || {}));

No comments: