A Brief Analysis of Namespace namespace Mode in JavaScript
- 2021-06-29 10:08:38
- OfStack
namespace is the Namespace, also known as Namespace and Namespace.Unlike C#or Java, JavaScript has special namespace and package syntax support. Naming conflicts can become a serious problem when JS is complex to a certain extent, especially when referencing a large number of third-party JS frameworks and libraries, so it is important to use JS's own variations to create namespaces.
Namespaces help reduce the number of global variables needed in your program and also help avoid naming conflicts or long name prefixes.
Examples of namespaces:
/**
* Create Global Objects MYAPP
* @module MYAPP
* @title MYAPP Global
*/
var MYAPP = MYAPP || {};
/**
* Returns the specified namespace and creates it if it does not exist.
* Note: Be careful when naming. Keep keywords in mind. It may be 1 Some browsers are unavailable.
*
* @method namespace
* @param {String *} At least need to be created 1 Namespaces
* @return {Object} Last 1 References to objects created by namespaces
*/
MYAPP.namespace = function(str){
var parts = 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(typeof parent[parts[i]] === "undefined"){
parent[parts[i]] = {};
}
parent = parent[parts[i]];
}
return parent;
}
/**
* bfun yes Basic Functions Extended Abbreviations for
* Role: Number function extensions including arrays, strings, etc.
*
* @module bfun
*/
MYAPP.bfun = {
array:(function(){
return {
/**
* @method isArray Determine if it is an array
* @param {Array} array
* @return {Boolean} True Return true Otherwise return false
*/
isArray: function(){
return Object.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 Return true Otherwise return undefined
*/
inArray: function(val,arr){
for(var i=0,l=arr.length;i<l;i++){
if(arr[i] === val){
return true;
}
}
}
}
})(),
string:(function(){
return {
/**
* @method trim Filter extra spaces on both sides of a string
* @param {String} Character string
* @return {String} Character string
*/
trim: function(){
return arguments[0].replace(/(^\s*)|(\s*$)/g, "");
},
/**
* @method ltrim Filter extra spaces to the left of a string
* @param {String} Character string
* @return {String} Character string
*/
ltrim: function(){
return arguments[0].replace(/^s+/g, "");
},
/**
* @method rtrim Filter extra spaces to the right of a string
* @param {String} Character string
* @return {String} Character string
*/
rtrim: function(){
return arguments[0].replace(/s+$/g, "");
}
}
})()
}
// test
MYAPP.test = {
init: function(){
// Reference first using the corresponding module
var marray = MYAPP.namespace("MYAPP.bfun.array");
var mstring = MYAPP.namespace("MYAPP.bfun.string");
var arr = ["a","b"];
var str = " abc ";
console.log(" Determine if it is an array: " + marray.isArray(arr));
console.log(" Is the value in the array: " + marray.inArray("a",arr));
console.log(" Filter left and right spaces: " + mstring.trim(str));
}
}
MYAPP.test.init();