oop - Javascript namespacing and OO organization -


i have javascript code follow:

$(document).ready(function () {      //call of global functions     globalfunction1();     globalfunction2(); //create new object inside     globalfunction3(); }  function globalfunction1() {     // directly jquery selectors     var testobj1 = new object1($('#tree')); // called later in function     testobj.dosomething(); }   function globalfunction2() {     // other things }  function globalfunction3() {     // directly jquery selectors }  //creating object in js var object1 = (function () {         var tree;          function object1($tree) {             tree = $tree;         }); } object1.prototype.dosomething = function () {     ..... }; return fancystructure; })(); 

normally have more global functions , if possible try create objects using new keyword (as in java or c#) now, asked provide namespacing in order avoid function conflict problems. thing not sure how achieve giving current code , knowing need keep code object oriented. hence, wondering if there way add namespacing effisciently. suggestion long along lines of adding namespace.

just put functions object:

var mynamespace = {     globalfunction1 : function() {         // directly jquery selectors         var testobj1 = new object1($('#tree')); // called later in function         testobj.dosomething();     },     globalfunction2 : function() {         // other things     },     globalfunction3 : function() {       // directly jquery selectors     } } 

and call functions with

mynamespace.globalfunction1(); 

or define namespace

mynamespace = {}; 

and later add the functions with

mynamespace.globalfunction1 = function() {     //do }; 

Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -