`
yitian_web
  • 浏览: 5161 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

js prototype 跟继承无关

阅读更多

 

var JS={};

JS.apply= function(o, c, defaults){

    if(defaults){

        JS.apply(o, defaults);

    }

    if(o && c && typeof c == 'object'){

        for(var p in c){

            o[p] = c[p];

        }

    }

    return o;

};

 

 

var haoren=function(cfg){

var b=0; //私有属性

var d=function(){//私有方法

alert(123);

}

JS.apply(this,{

y:8,//公共属性

h:9,

haorenMethod:function(){//公共

alert("super.haorenMethod");

},

haorenMethod_1:function(){//公共

alert("super.haorenMethod_1");

}

});

JS.apply(arguments.callee.prototype,{//公共一次方法

q:8,

w:9,

haorenMethod2:function(){

alert("super.haorenMethod2");

},

haorenMethod2_1:function(){

alert("super.haorenMethod2_1");

}

});

}

 

 

var haoren1=function(cfg){

haoren.call(this,arguments);//继承 z_z父类

JS.apply(arguments.callee.prototype,haoren.prototype); //继承 z_z父类.prototype

var b=0; //私有属性

}

 

 

var haoren2=function(cfg){

haoren1.call(this,arguments);//继承 z_z父类

JS.apply(arguments.callee.prototype,haoren1.prototype); //继承 z_z父类.prototype

var b=0; //私有属性

 

var d=function(){//私有方法

alert(123);

}

JS.apply(this,{

u:8,//公共属性

k:9,

haorenMethod_1:function(){//公共

alert("2b:"+b+"  u:"+this.u+" w:"+this.w+"  s:"+this.s+" y:"+this.y);

}

});

JS.apply(arguments.callee.prototype,{//公共一次方法

a:cfg.a,

s:9,

haorenMethod2_1:function(){

alert("2haorenMethod2_1");

}

});

}

var t=new haoren2({a:3});

var t1=new haoren2({a:4});

 

t.haorenMethod()

t.haorenMethod_1();

t.haorenMethod2();

t.haorenMethod2_1();

alert(t.a);//4

alert(t1.a);//4

引用标出处      
如果我理解的有误 请指正 谢谢 yitian_web@hotmail.com

想给js (面象对象)  写一个模板 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics