A brief analysis of _javascript techniques of namespace namespace pattern in JavaScript

Source: Internet
Author: User

namespace is called "namespaces", also known as "namespace", "namespace". JavaScript is not like C # or Java, there are special namespace and package syntax support, when JS complexity to a certain extent, especially the reference to a large number of Third-party JS framework and Class library, naming conflicts will become a serious problem, So it's important to use JS's own workaround to build namespaces.

Namespaces help reduce the number of global variables required in your program, and also help avoid naming conflicts or too long prefix prefixes.

For examples of namespaces:

/** * Create global Object MYAPP * @module MYAPP * @title MYAPP Global * * var MYAPP = MYAPP | |
{};
/** * Returns the specified namespace and creates a namespace if the namespace does not exist.
* Note: The name should be careful, note that the retention of keywords, may be in some browsers can not be used. * * @method namespace * @param {String *} must create at least one namespace * @return {object} references to objects created in the last namespace/myapp.namespace = function (s TR) {var parts = str.split ("."), parent = MYAPP, i=0, l=0; if (parts[0]=== "MYAPP") {parts = Parts.slice (1);} for (I=0,l=par Ts.length; i<l;i++) {if (typeof parent[parts[i]] = = = "undefined") {Parent[parts[i]] = {};} parent = parent[parts[i]];
Ent  The/** * Bfun is the abbreviation for basic functions extended: Includes array, string, and so on number function extension * * @module bfun/myapp.bfun = {array: (function () {return {/** * @method IsArray to determine whether the array * @param {array} (* @return {Boolean} True returns True, otherwise return false */isarray:function () {returns 
Object.prototype.toString.call (arguments[0]) = = ' [Object Array] '; /** * @method InArray checks whether the value is * @param {Value,array} value in the array, and the array * @return {Boolean} returns true, otherwise it returns undefined/inarray:funct Ion (Val,arr) {for (Var I=0,l=arr. length;i<l;i++) {if (arr[i] = = val) {return true;}}}  } (), String: (function () {return {/** * @method trim filters extra space on both sides of the string * @param {string} String * @return {string} string/Trim: function () {return arguments[0].replace (/^\s*) | ( \s*$)/g, "");},/** * @method LTrim the extra space on the left of the filter string * @param {string} String * @return {string} string/Ltrim:function () {retur N Arguments[0].replace (/^s+/g, "");},/** * @method RTrim The extra space on the right side of the filter string * @param {string} String * @return {string} string/RT Rim:function () {return arguments[0].replace (/s+$/g, "");}}) ()///Test myapp.test = {init:function () {///use the corresponding module to first refer to var Marray = Myapp.namespace ("MYAPP.bfun.array"); var mstring =
Myapp.namespace ("MYAPP.bfun.string");
var arr = ["A", "B"];
var str = "ABC";
Console.log ("Judge whether the array:" + Marray.isarray (arr));
Console.log ("value is in array:" + Marray.inarray ("a", arr));
Console.log ("Filter the Left and right spaces:" + Mstring.trim (str)); } MYAPP.test.init ();

The above is a small set of JavaScript in the description of the namespace namespace model of all the narrative, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

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.