zoukankan      html  css  js  c++  java
  • javascipt自定义命名空间、静态类、实例对象

    <script type="text/javascript">
        //创建命名空间:jSite.Import(namespace)
        //创建命名空间的同时执行方法过程和实例对象
        //引用:var o=jS.using(namespace,class);或var o=jS.using(namespace);
        //实例:var o=jS.using('System.Images');
        (function(window, undefined) {
        var document = window.document, navigator = window.navigator, location = window.location;
        var jSite = (function() {
            // 构建jSite对象 
            var jSite = function(selector, context) {
                return new jSite.fn.init(selector, context, rootjSite)
            }
            jSite.fn = jSite.prototype = {
                constructor: jSite,
                init: function(selector, context, rootjSite) {//jSite是一个函数,函数返回值是一个对象,并且有很多实例方法,这些方法用来操作相关节点
                },
                jsite: "1.1.1",
                length: 0,
                size: function() {
                    return this.length;
                }
            };
            jSite.fn.init.prototype = jSite.fn; 
            //___________________________________工厂 开始______________________________________
            jSite.extend = jSite.fn.extend = function() {//静态方法-工厂(合并对象直接量为target对象)
               var target = arguments[0] || {}, // 第一个参数是目标  
               i = 1, length = arguments.length, deep = false, options;    
               if (target.constructor == Boolean) {// 第一个参数是bool型的    
                    deep = target;// 深度copy   
                    target = arguments[1] || {};// target指向第二个参数    
                    i = 2;  
                }    
                if (typeof target != "object" && typeof target != "function")target = {};// target 是string 型的或?,则设为空对象      
                if (length == i) {// 只有一个参数?或deep copy 时,两个参数    
                    target = this;// 目标为this    
                    --i;  
                }    
                for (;i < length; i++)   
                if ((options = arguments[i]) != null)        
                for (var name in options) {       
                    var src = target[name], copy = options[name];//复制对象       
                    if (target === copy)// 防止死循环         
                    continue;       
                    if (deep && copy && typeof copy == "object" && !copy.nodeType)// 深度复制处理,最深为元素             
                    target[name] = jQuery.extend(deep, src || (copy.length != null ? [] : {}), copy);
                    else if (copy !== undefined)// 直接复制         
                    target[name] = copy;       
                 }    
                 return target;//返回一个新的target对象
            };
            jSite.Import = function() {  //绑定命名空间[创建命名空间]
                var a=arguments, o=null, i, j, d;    
                for (i=0; i<a.length; i=i+1) {        
                    d=a[i].split(".");        
                    o=jSite;        
                    for (j=(d[0] == "jSite") ? 1 : 0; j<d.length; j=j+1) {            
                        o[d[j]]=o[d[j]] || {};            
                        o=o[d[j]];        
                        }    
                    }    
                    return o;
            };
            jSite.using=function(n,o,override){//命名空间与静态方法合并
                var ns=jSite.Import(n),obj={};
                if(typeof(o) != 'object'||o == 'undefined')o=ns;
                ns=function(){};
                for(var i in o){
                    if(o.hasOwnProperty(i) && (!obj.hasOwnProperty(i) || override))
                    obj[i] = o[i];
                }
               ns.prototype=obj;
               return new ns;
             }
            //____________________________________工厂 结束____________________________________________
            //扩展jSite对象方法
            jSite.fn.extend({
                method: function() {
                    alert("静态方法扩展的实现");
                }
            });
            jSite.extend({
                ext:function(){
                    alert("静态方法的实现");
                }
            });
            jSite.System={
                Images:{
                    img:'图片方法的实现'
                },
                Table:{
                    tbl:'表格方法的实现'
                }
            }
            //返回jSite实例对象 
            return jSite
        })();
        window.jSite = window.jS = jSite;
        if (typeof define === "function" && define.amd && define.amd.jSite) {
            define("jSite", [],
            function() {
                return jSite
            })
            }
        })(window);
         var o=jSite.using('System.Images')
         alert(o.img);
        </script>

     下载附件

  • 相关阅读:
    微软外服 AlI In One
    js 循环多次和循环一次的时间的性能对比 All In One
    vue inject All In One
    Excel 表格数据倒置 All In One
    SVG tickets All In One
    OH MY ZSH All In One
    js array for loop performance compare All In One
    mac terminal show You have new mail All In one
    新闻视频 26 制作母版页
    转自牛腩 母版页和相对路径
  • 原文地址:https://www.cnblogs.com/sntetwt/p/2992199.html
Copyright © 2011-2022 走看看