JavaScript中のnamespace

10218 ワード

 1  <head>
 2   <title> New Document </title>
 3   <script>
 4   var global = window.global||{};
 5   global.version = '1.1';
 6   global.author = 'SL';
 7   global.namespace = function (_namespace) {
 8     var levels = _namespace.split('.'); 
 9     var obj = global; 
10     for (var i = (levels[0] == 'gloabl') ? 1 : 0; i < levels.length; ++i) { 
11         obj[levels[i]] = obj[levels[i]] || {}; 
12         obj = obj[levels[i]]; 
13         } 
14     };
15   global.namespace('test');
16   global.test.init = function(){
17   alert("123");
18   }
19   window.onload=  global.test.init;
20   </script>
21   </head>
22  <body>
23 hello,world?!
24  </body>
25 </html>
上は自分で書いたのです.そしてネットでよく書いたのを見ました.JavaScriptについてよく知っている人だと思います.(http://www.cnblogs.com/kuikui/archive/2013/07/14/3190381.html)
 
/**
*       MYAPP
* @module MYAPP
* @title MYAPP Global
*/
var MYAPP = MYAPP || {};

/**
*          ,                。
*   :      ,       ,            。
*
* @method namespace
* @param {String *}             
* @return {Object}                 
*/
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 Basic Functions Extended   
*   :    、          
*
* @module bfun
*/
MYAPP.bfun = {
    array:(function(){
        return {
            /**
            * @method isArray        
            * @param {Array}   
            * @return {Boolean}    true,    false
            */
            isArray: function(){
                return Object.prototype.toString.call(arguments[0])  === '[object Array]'; 
            },
            /**
            * @method inArray          
            * @param {value,Array}  ,  
            * @return {Boolean}    true,    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             
            * @param {String}    
            * @return {String}    
            */
            trim: function(){
                return arguments[0].replace(/(^\s*)|(\s*$)/g, "");
            },
            /**
            * @method ltrim             
            * @param {String}    
            * @return {String}    
            */
            ltrim: function(){
                return arguments[0].replace(/^s+/g, "");
            },
            /**
            * @method rtrim             
            * @param {String}    
            * @return {String}    
            */
            rtrim: function(){
                return arguments[0].replace(/s+$/g, "");
            }
        }
    })()
}

//   
MYAPP.test = {
    init: function(){
        //           
        var marray = MYAPP.namespace("MYAPP.bfun.array");
        var mstring = MYAPP.namespace("MYAPP.bfun.string");

        var arr =  ["a","b"];
        var str = "   abc  ";

        console.log("       :" + marray.isArray(arr));
        console.log("       :" + marray.inArray("a",arr));
        console.log("      :" + mstring.trim(str));
    }
}

MYAPP.test.init();