window.qv = {
    pkg: function(name,fn) {
        var target = window,packageNS =name.split(".");
        //创建命名空间
        for(var index=0;index<packageNS.length - 1;index++){
            if (!(packageNS[index] in target)) {
                target[packageNS[index]] = {};
            }
            target = target[packageNS[index]];
        }
        //创建构造方法,来源于construct,这里要注意js的this对象的指向,this只有到最后方法执行的那一刻才能明确指向
        target[packageNS[index]] = function() {
            if (typeof(this.construct) == 'function') {
                this.construct.apply(this, arguments);
            }
        };
        //target[packageNS[index]]=new parent();
        fn.call(target[packageNS[index]].prototype);
    }
};

QV库的pkg方法,执行fn方法来创建name对象。

        qv.pkg('qq.vip.test', function(){
            _public=this;
            _public.construct=function(name){
                this.name=name
            }
            _public.show=function(){
                alert(this.name);
            };
        });
        new qq.vip.test("yinshen").show();

 

相关文章:

  • 2021-06-22
  • 2021-11-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-19
  • 2021-10-25
猜你喜欢
  • 2021-08-03
  • 2021-07-22
  • 2022-01-14
  • 2021-11-13
  • 2021-12-07
  • 2021-10-11
  • 2022-01-18
相关资源
相似解决方案