首页 新闻 会员 周边

js的继承和扩展可以这样写么?请高手指点......

0
悬赏园豆:10 [待解决问题]

/*
扩展的本地方法
*/
var Natives={
    //继承
    extend: function(parentClass){
        for (var p in parentClass.prototype){
            //继承父类中子类没有的
            if(!this.prototype[p]){this.prototype[p]=parentClass.prototype[p];
            }else{
                this.prototype.parent=this.prototype.parent?this.prototype.parent:{};
                this.prototype.parent[p]=parentClass.prototype[p];
            }
        }
         
    },
    //扩展
    implement: function(params){
         for (var p in params){
            //扩展父类的原型 会覆盖父类
            this.prototype[p]=params[p];           
        }
    }
};
//添加Function 对象 的本地方法
(function(){
  for(var p in Natives){
      Function.prototype[p]=Natives[p];
  }
 })();
function Class(params){
    var newClass = function(){
        this.init.apply(this,arguments);
    };
    newClass.constructor = Class;
    newClass.prototype=params;
    newClass.prototype.constructor = newClass;
    return newClass;
}
//创建人类
var person=new Class({                     
    name:"fff",
    Options:{sex:1},
    init:function(a,b){
        alert(this.name);
    },
    say:function(){
        alert(this.name);
    }   
});
var teacher=new Class({
    name:"teacher",
    init:function(){
        //alert(this.name);
    },
    speack:function(){
        alert("i'm teacher");
    }
});
teacher.extend(person);
teacher.implement({
    play:function(){
        alert("i can play");
    }
});
var tt=new teacher();
    tt.say();</p>

web-js的主页 web-js | 初学一级 | 园豆:10
提问于:2010-10-09 17:39
< >
分享
所有回答(1)
0

哥们, 你这么写, 谁能看明白? 

你看看我的这个Blogs看看能不能找到答案

http://www.cnblogs.com/yangboyu/archive/2010/08/31/1813030.html

13路易的 | 园豆:215 (菜鸟二级) | 2010-10-09 19:19
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册