The $. Extend method of jquery needs to be referenced in this method. Therefore, jquery needs to be referenced first. The version does not matter. You can write this method in the public module.
Source code:
<SCRIPT type = "text/JavaScript" src = "JS/jquery-1.7.2.min.js"> </SCRIPT>
// NS can be a string or a specified object // if it is defined for the first time, it must be used. the function creates the corresponding space function def_name_space (NS, OP) {Switch (typeof NS) {Case 'object': If (NS = NULL) throw new error ('Param error'); jquery. extend (NS, OP); break; Case 'string': var NSS = NS. split (". "); var part = Window; for (I in NSS) {var name = NSS [I]; If (! Part [name]) part [name] = new object (); Part = part [name];} jquery. extend (part, OP); break; default: Throw new error ('Param error'); break ;}}
Easy to use
DEF_NAME_SPACE("zh",{ dotest:function(){ alert('zh.dotest'); } }); DEF_NAME_SPACE("zh.mySpace",{ dotest:function(){ alert('zh.mySpace.dotest'); } });
Of course, the difference is that this method is used to define the namespace. The content in the namespace should actually be a JSON expression.