多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## js实现继承 第一种方法:被继承的对象必须是object ~~~ //animal 是 object var animal = { type : "animal", runtype : "by foot", run : function(){ alert(this.type + " is runing by"+ this.runtype); } } // bird 是 function; var bird = function(){ }; //把 bird 的 prototype(原型) 赋值为 animal bird.prototype = animal; var xiaoming = new bird(); xiaoming.type = "xiaoming" xiaoming.runtype = "fly" xiaoming.run(); ~~~ * * * * * 第二种方法:被继承对象必须是function ~~~ var animal = function(){ this.type = "animal"; this.runtype = "by foot"; this.run = function(){ alert(this.type + " is runing "+ this.runtype); } } var bird = function(){ animal.call(this);//用animal覆盖bird,多继承就多覆盖几次 //animal.apply(this);//与上面作用相同 this.type = "bird"; this.runtype = "by fly"; }; var xiaoming = new bird(); xiaoming.type = "xiaoming" xiaoming.runtype = "fly" xiaoming.run(); ~~~ apply与call的区别 ~~~ animal.call(this,arg1,arg2,arg3); animal.apply(this,array(arg1,arg2,arg3)); ~~~