/** * Create global objects myapp* @module myapp* @title MYAPP Global*/varMyApp = MyApp | | {};/** * Returns the specified namespace, or creates a namespace if the namespace does not exist. * Note: Be careful when naming, keep keywords in mind, and may not be available in some browsers. * * @method namespace* @param {String *} need to create at least one namespace * @return {object} The last namespace creates a reference to the objects*/Myapp.namespace=function(str) {varParts = Str.split (".")), Parent=MYAPP, I=0, L=0; if(parts[0]=== "MYAPP") {Parts= Parts.slice (1); } for(I=0,l=parts.length; i<l;i++){ if(typeofParent[parts[i]] = = = "undefined") {Parent[parts[i]]= {}; } Parent=Parent[parts[i]]; } returnparent;}/** * Bfun is the abbreviation of basic Functions extended * Function: include array, string, etc. function extension * * @module bfun*/Myapp.bfun={array: (function(){ return { /** * @method IsArray to determine if an array * @param {array} arrays * @return {Boolean} True returns True, otherwise false */IsArray:function(){ returnObject.prototype.toString.call (arguments[0]) = = = ' [Object Array] '; }, /** * @method inArray Check if the value is in the array * @param {Value,array} value, array * @return {Boolean} true returns True , otherwise return undefined*/InArray:function(Val,arr) { for(vari=0,l=arr.length;i<l;i++){ if(Arr[i] = = =val) { return true; }}}) (), String: (function(){ return { /** * @method trim filters extra spaces on both sides of the string * @param {string} String * @return {string} string */Trim:function(){ returnArguments[0].replace (/(^\s*) | ( \s*$)/g, ""); }, /** * @method ltrim filter string left extra space * @param {string} String * @return {string} string */LTrim:function(){ returnArguments[0].replace (/^s+/g, ""); }, /** * @method RTrim filter string to the right extra space * @param {string} String * @return {string} string */RTrim:function(){ returnArguments[0].replace (/s+$/g, ""); } } })()}//TestMyapp.test ={init:function(){ //use the corresponding module to first reference varMarray = Myapp.namespace ("MYAPP.bfun.array"); varmstring = Myapp.namespace ("MYAPP.bfun.string"); vararr = ["A", "B"]; varstr = "ABC"; Console.log ("Determine if the array:" +Marray.isarray (arr)); Console.log ("Value is in array:" + Marray.inarray ("a", arr)); Console.log ("Filter left and right spaces:" +Mstring.trim (str)); }}myapp.test.init ();
Write a few useful functions
Queryselector and Queryselectorall are the new query interface provided by the information, but the name is very long, write a simple, innerHTML attribute is also commonly used to write a simple version of the HTML method of imitating jquery
(function () { var_ns =function() {} _ns.prototype.select=function(selector,context) {varContext = Context | |document; returnContext.queryselectorall (selector); } _ns.prototype.isarraylike=function(obj) {if(objinstanceofArray) { return true; } varLength=obj.length; if(Obj.nodetype = = 1 &&length) { return true; } return false; } _ns.prototype.html=function(obj,value) {varisarray= This. Isarraylike (obj), i=0; if(typeofValue = = ' String ') { if(!IsArray) {obj.innerhtml=value; } Else { varLength =obj.length; while(I <length) {obj[i].innerhtml=value; I+ = 1; } } } Else { if(!IsArray) { returnobj.innerhtml; } Else { returnObj[0].innerhtml; }}} window. NS=New_ns (); })();
Some knowledge of constructor functions
To achieve some of the above basics, in addition to prototype, you need to know a little bit about JavaScript constructors.
1. What kind of function is a constructor
In the JavaScript world, constructors are not mystical or special, and any function called by the new operator can be made into a constructor, instead of using the new operator, it is not a constructor, but rather a direct call to a normal function.
2. What results The constructor returns
The return value of a constructor is divided into two cases, returning an anonymous object created by new when the function has no return statement or return to a base type (Bool,int,string,undefined,null), which is the function instance If the Function body returns a reference type Object (Array,function,object, etc.), the object overrides the anonymous object created by new as the return value.
Write a small example to verify
Instance application of the JavaScript namespace