var $ = new (function _$(){
this.extend = function (dest,src,force){
for(var name in src){
if(!force && dest[name]){
alert(name);
continue;
}
dest[name] = src[name];
}
};
})();$.extend(Object.prototype,{
extends:function(clazz){
//将父类保存起来
this._super = clazz;
return this;
},
super:function(){
//包含对父类的引用,同时将父类的方法和属性
var args = Array.prototype.slice.apply(arguments);
this.super = this._super.apply(this,args);
$.extend(this,this.super,true);
}
});
function A(name,value){
this.name = name;
this.value = value;
}A.prototype.test = function(){
alert('a.test');
}
function B(){
this.extends(A).super({a:'a'},'bb');
alert(this.name.a);
}
var b = new B();

解决方案 »

  1.   

    刚才发现一点问题,super的封装应该这样就OK了,不然super为空
    var args = Array.prototype.slice.apply(arguments);
    this._super.apply(this.super,args);
    //$.iteratorTest(this.super);
    $.extend(this,this.super,true);
      

  2.   

    晚上回来又续写了,不重新开贴了就,功能一点点加,现在的父类调用还存在问题var $util = new (function(){
    //指定class是否已加载
    this.classExist = function(clazz){
    return !(!clazz.prototype.type);
    }
    //扩展
    this.extend = function (dest,src,force,scope,filter){
    for(var name in src){
    if(!force && dest[name]){   
      continue;
    }
    if(scope){
    src[name].scope = scope;
    }
    if(filter){
    if(!filter(src[name])){
    continue;
    }
    }
    dest[name] = src[name];
    }
       };
       //继承
       this.inherit = function(current,parent){
       this.extend(current,parent.prototype,true,null,function(obj){
       return obj.scope === "public";
       });
       current.constructor.prototype = current;
       };
       //遍历一个对象中的属性,调试用
       this.iterateTest = function(obj){
       for(var name in obj){  
       alert(name);
       }
       };
       //判断是否为函数
       this.isFn = function(obj){
       return typeof(obj) === "function";
       };
    })();
    //基类型
    function Class(){}
    //为基类型添加属性和方法
    $util.extend(Class.prototype,{
    _type:"Class",//类名
    getClass:function(){//获取类名
    return this._type;
    },
    inited:false,//已经初始化标志
    public:function(obj){//公共作用域
    $util.extend(this.constructor.prototype,obj,true,'public');
    if(!this.inited){
    $util.extend(this,obj,true);
    }
    },
    private:function(obj){//私有作用域
    $util.extend(this.constructor.prototype,obj,true,'private');
    if(!this.inited){
    $util.extend(this,obj,true);
    }
    },
    classend:function(){//类定义结束符
    this.inited = true;
    }
    },true);
    //基类模板加载
    new Class();
    //=============================Utilities END=========================
    //=============================Prototype=========================
    //为函数添加作用域
    $util.extend(Function.prototype,{
    scope:"public"
    });//为对象添加继承方法
    $util.extend(Object.prototype,{
    extends:function(parent){
    if(!parent){
    parent = Class;
    }
    if(!$util.classExist(parent)){
    new parent();
    }
    if(!this.inited){
    var reg = /function\s([a-zA-z]+)\(/;
    this._type=this.constructor.prototype._type=this.constructor.toString().match(reg)[1];
    $util.inherit(this,parent);
    }
    }
    });
    //=============================Prototype END========================
    //=============================Test========================
    function A(){
    this.extends(Class);//继承自基类
    //公共属性和方法
    this.public({
    test:function(){
    alert('test');
    }
    });
    //私有属性和方法
    this.private({
    test1:function(){
    alert('test1');
    }
    });
    this.classend();//类定义结束
    }function B(){
    this.extends(A);//继承自A类
    this.classend();//类定义结束
    }new B().test();
    //=============================Test END========================
      

  3.   


    想实现一个类java的基本架构,支持作用域private,public,支持父类访问,支持接口,暂时向这个目标努力了
      

  4.   

    public和extends在js中指关键字,楼主需要修改一下封装,如下:
    var $util = new (function(){
        //指定class是否已加载
        this.classExist = function(clazz){
            return !(!clazz.prototype.type);
        }    
        //扩展
        this.extend = function (dest,src,force,scope,filter){
            for(var name in src){
                if(!force && dest[name]){              
                  continue;
                }
                if(scope){
                    src[name].scope = scope;
                }
                if(filter){
                    if(!filter(src[name])){
                        continue;
                    }
                }
                dest[name] = src[name];
            }
          };
          //继承
          this.inherit = function(current,parent){
              this.extend(current,parent.prototype,true,null,function(obj){
                  return obj.scope === "public";
              });
              current.constructor.prototype = current;
          };
          //遍历一个对象中的属性,调试用
          this.iterateTest = function(obj){
              for(var name in obj){              
                  alert(name);    
              }
          };
          //判断是否为函数
          this.isFn = function(obj){
              return typeof(obj) === "function";    
          };
    })();
    //基类型
    function Class(){}
    //为基类型添加属性和方法
    $util.extend(Class.prototype,{
        _type:"Class",//类名
        getClass:function(){//获取类名
            return this._type;    
        },
        inited:false,//已经初始化标志
        Public:function(obj){//公共作用域
            $util.extend(this.constructor.prototype,obj,true,'public');    
            if(!this.inited){
                $util.extend(this,obj,true);
            }
        },
        Private:function(obj){//私有作用域
            $util.extend(this.constructor.prototype,obj,true,'private');
            if(!this.inited){
                $util.extend(this,obj,true);
            }
        },
        classend:function(){//类定义结束符
            this.inited = true;    
        }
    },true);
    //基类模板加载
    new Class();
    //=============================Utilities END=========================
    //=============================Prototype=========================
    //为函数添加作用域
    $util.extend(Function.prototype,{
        scope:"public"    
    });//为对象添加继承方法
    $util.extend(Object.prototype,{
        Extends:function(parent){
            if(!parent){
                parent = Class;
            }
            if(!$util.classExist(parent)){            
                new parent();
            }
            if(!this.inited){
                var reg = /function\s([a-zA-z]+)\(/;
                this._type=this.constructor.prototype._type=this.constructor.toString().match(reg)[1];    
                $util.inherit(this,parent);
            }
        }
    });
    //=============================Prototype END========================
    //=============================Test========================
    function A(){    
        this.Extends(Class);//继承自基类
        //公共属性和方法
        this.Public({
            test:function(){
                alert('test');
            }
        });
        //私有属性和方法
        this.Private({
            test1:function(){
                alert('test1');
            }        
        });    
        this.classend();//类定义结束
    }function B(){
        this.Extends(A);//继承自A类
        this.classend();//类定义结束
    }new B().test();
    //=============================Test END========================
      

  5.   

    谢谢了,之前的Firefox中试过那个没有问题,不过今天起来还是改过了,我把它改成$public和$private,$super了,不过你的更加直观一点,谢谢了
      

  6.   

    不再开贴了,继续奉上,没有采用上面说的大写保留字的做法,因为怕误写,而是使用$public代替,目前已经实现继承父类,且仅继承公共方法,可以调用父类的构造器,下一步,实现接口机制。不多说了,上代码
    //=============================Utilities=========================
    var $util = new (function(){
    //指定class是否已加载
    this.classExist = function(clazz){
    return !(!clazz.prototype._type);
    };
    this.findClass=function(clazz){
    return this.classList[clazz];
    };
    //初始化
    this.initClass=function(type){
    if(!this.initing.status){
    this.initing.status=true;
    this.initing.type=type;
    }
    };
    this.endInitClass=function(){
    this.initing.status=false;
    };
    this.initing={status:false,type:"Class"};
    //扩展
    this.extend = function (dest,src,force,scope,filter){
    for(var name in src){
    if(name != "_type" && name != "inited" && (force || (!force && dest[name])) && (!filter || (filter&&filter(src[name]))) ){   
      if(scope){
    src[name].scope = scope;
    }
    dest[name] = src[name];
    }
    }
    };
    //继承
    this.inherit = function(current,parent){
    this.extend(current,parent.prototype,true,null,function(obj){
    return obj.scope && $util.isFn(obj)?obj.scope === "$public":true;
    });

    current._super.fn=parent.prototype._constructor.fn;
    current.constructor.prototype = current;
    };
    //遍历一个对象中的属性,调试用
    this.iterateTest = function(obj){
    for(var name in obj){  
    alert(name);
    }
    };
    //判断是否为函数
    this.isFn = function(obj){
    return typeof(obj) === "function";
    };
    })();
    //=============================Utilities END=========================
    //=============================BaseClass============================
    function Class(){}
    //为基类型添加属性和方法
    $util.extend(Class.prototype,{
    _type:"Class",//类名
    getClass:function(){//获取类名
    return this._type;
    },
    inited:false,//已经初始化标志
    $public:function(obj){//公共作用域
    if(this.inited){
    return;
    }
    $util.extend(this.constructor.prototype,obj,true,'$public');
    $util.extend(this,obj,true);
    },
    $private:function(obj){//私有作用域
    if(this.inited){
    return;
    }
    $util.extend(this.constructor.prototype,obj,true,'$private');
    $util.extend(this,obj,true);
    },
    _constructor:{args:null,fn:function(){}},
    $constructe:function(fn){//构造器
    if(this.inited){
    return;
    }
    this._constructor.fn=this.constructor.prototype._constructor.fn = fn;
    },
    $super:function(){//调用父类的方法
    this._super.fn.apply(this,Array.prototype.slice.apply(arguments));
    },
    _super:{fn:function(){},args:null},
    classend:function(){//类定义结束符
    //如果是执行类初始化过程,不执行构造器
    this.inited=true;
    if($util.initing.type===this._type){
    this._constructor.fn.apply(this,this._constructor.args);
    }
    }
    },true);
    //基类模板加载
    new Class();
    //=============================BaseClass============================
    //=============================Prototype=========================
    //为函数添加作用域
    $util.extend(Function.prototype,{
    scope:"$public"
    });//为对象添加继承方法
    $util.extend(Object.prototype,{
    $extends:function(parent){
    //如果类没有初始化过
    if(!this.inited){
    if(!parent){
    parent = Class;
    }
    var reg = /function\s([a-zA-z]+)\(/;//获取类名
    this._type=this.constructor.prototype._type=this.constructor.toString().match(reg)[1];//初始化类型
    $util.initClass(this._type);
    if(!$util.classExist(parent)){
    new parent();
    }
    $util.inherit(this,parent);
    }
    //保存构造器参数
    this._constructor.args=Array.prototype.slice.apply(this.constructor.arguments);
    }
    },true);
    //=============================Prototype END========================
    //=============================Test========================
    function A(){
    this.$extends(Class);//继承自基类
    this.$constructe(function(a){
    this.name=a;
    });

    //公共属性和方法
    this.$public({
    test:function(){
    alert('test');
    }
    });
    //私有属性和方法
    this.$private({
    test1:function(){
    alert('test1');
    }
    });
    this.classend();//类定义结束
    }
    function B(){
    this.$extends(A);//继承自A类,且仅有A类的public 方法
    this.$constructe(function(a,b){
    this.$super(a);
    //this.test1();//not run
    });

    this.classend();//类定义结束
    }
    var b = new B("test");
    b.test();
    //=============================Test END========================