About the namespace namespace pattern in JS

Source: Internet
Author: User
Tags rtrim

Namespaces help to reduce the number of global variables required in your program, and it helps to avoid naming conflicts or too long prefix names.

An example of a namespace:

1 /**2 * Create global object MyApp3 * @module MYAPP4 * @title MYAPP Global5 */6 varMyApp = MyApp | | {};7 8 /**9 * Returns the specified namespace, or creates a namespace if the namespace does not exist. Ten * Note: Be careful when naming, keep keywords in mind, and may not be available in some browsers.  One * A * @method namespace - * @param {String *} requires at least one namespace to be created - * References to objects created by the last namespace @return {object} the */ -Myapp.namespace =function(str) { -     varParts = Str.split (".")), -Parent =MYAPP, +I=0, -L=0; +  A     if(parts[0]=== "MYAPP"){ atParts = Parts.slice (1); -     } -      for(I=0,l=parts.length; i<l;i++){ -         if(typeofParent[parts[i]] = = = "undefined"){ -Parent[parts[i]] = {}; -         } inParent =Parent[parts[i]]; -     } to     returnparent; + } -  the /** * * Bfun is the abbreviation of basic Functions extended $ * Function: including array, string and so on function extensionPanax Notoginseng * - * @module Bfun the */ +Myapp.bfun = { AArray: (function(){ the         return { +             /** - * @method IsArray to determine if an array $ * @param an array of {array} $ * @return {Boolean} True returns True, otherwise it returns false -             */ -IsArray:function(){ the                 returnObject.prototype.toString.call (arguments[0]) = = = ' [Object Array] ';  -             },Wuyi             /** the * @method InArray Check if the value is in the array - * @param {Value,array} value, array Wu * @return {Boolean} True returns True, otherwise returns undefined -             */ AboutInArray:function(Val,arr) { $                  for(vari=0,l=arr.length;i<l;i++){ -                     if(Arr[i] = = =val) { -                         return true; -                     } A                 } +             } the         } -     })(), $String: (function(){ the         return { the             /** the * @method Trim filters extra spaces on both sides of the string the * @param {string} string - * @return {string} string in             */ theTrimfunction(){ the                 returnArguments[0].replace (/(^\s*) | ( \s*$)/g, ""); About             }, the             /** the * @method LTrim filter string to the left of the extra space the * @param {string} string + * @return {string} string -             */ theLTrimfunction(){Bayi                 returnArguments[0].replace (/^s+/g, ""); the             }, the             /** - * @method RTrim filter string to the right of the extra space - * @param {string} string the * @return {string} string the             */ theRTrimfunction(){ the                 returnArguments[0].replace (/s+$/g, ""); -             } the         } the     })() the }94  the //Test theMyapp.test = { theInit:function(){98         //use the corresponding module to first reference About         varMarray = Myapp.namespace ("MYAPP.bfun.array"); -         varmstring = Myapp.namespace ("MYAPP.bfun.string");101 102         vararr = ["A", "B"];103         varstr = "ABC";104  theConsole.log ("Determine if an array:" +Marray.isarray (arr));106Console.log ("value is in array:" + Marray.inarray ("a", arr));107Console.log ("Filter left and right spaces:" +Mstring.trim (str));108     }109 } the 111MYAPP.test.init ();

About the namespace namespace pattern in JS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.