javascript - Jquery name space functional process is not understandable -
i searching across web jquery.namespace
process. got answer in stack overflow example script..
jquery.namespace = function() { var a=arguments, o=null, i, j, d; (i=0; i<a.length; i=i+1) { d=a[i].split("."); o=window; (j=0; j<d.length; j=j+1) { o[d[j]]=o[d[j]] || {}; o=o[d[j]]; console.log(o); } } // console.log(o); //object {} return o; }; // definition jquery.namespace( 'jquery.debug' ); jquery.debug.test1 = function() { alert( 'test1 function' ); }; jquery.debug.test2 = function() { alert( 'test2 function' ); }; // usage jquery.debug.test1(); jquery.debug.test2();
it has 2 parts, once "jquery.namespace" - function , declaring new methods name space. unable understand "jquery.namespace" function here.. tried understand line line, couldn't process function here..
any 1 explain me function, how that's works? or other easy method make name spacing using jquery..
thanks in advance
the function taking arguments list of namespaces want created.
lets simplify takes 1 namespace
jquery.namespace = function(namespace) { var o=null, j, d; d=namespace.split("."); o=window; (j=0; j<d.length; j=j+1) { o[d[j]]=o[d[j]] || {}; o=o[d[j]]; } } return o; };
the function simple. happening starting window recursively checking if objects have declared in our namespace exist. example take call jquery.namespace('foo.bar')
function check see if window had attribute foo
, if not create it. function check if window.foo
had attribute bar
, if not create it.
in end function create object linked window (a global variable) has relevant sub-objects. assigning functions , variables nested object rather directly window otherwise global variables.
Comments
Post a Comment