脚本宝典收集整理的这篇文章主要介绍了javascript代码实例教程-JavaScript面向对象编程深入分析,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。
Javascript 面向对象编程:构造函数的继承
本节主要介绍,如何生成一个"继承"多个对象的实例。
比如,现在有一个"动物"对象的构造函数,
functionAnimal(){ this.sPEcies="动物";
}
functionCat(name,color){ this.name=name;
this.color=color; }
functionCat(name,color){ Animal.apply(this,arguments);
this.name=name; this.color=color;
} VARcat1=newCat("大毛","黄色");
alert(cat1.species);//动物
Cat.PRototype=newAnimal(); Cat.prototype.constructor=Cat;
varcat1=newCat("大毛","黄色"); alert(cat1.species);//动物
Cat.prototype=newAnimal();
Cat.prototype.constructor=Cat;
o.prototype={};
o.prototype.constructor=o;
functionAnimal(){} Animal.prototype.species="动物";
Cat.prototype=Animal.prototype; CatCat.prototype.constructor=Cat;
varcat1=newCat("大毛","黄色"); alert(cat1.species);//动物
Cat.prototype.constructor=Cat;
alert(Animal.prototype.constructor);//Cat
varF=function(){}; F.prototype=Animal.prototype;
Cat.prototype=newF(); Cat.prototype.constructor=Cat;
alert(Animal.prototype.constructor);//Animal
functionextend(Child,Parent){
varF=function(){}; F.prototype=Parent.prototype;
Child.prototype=newF(); Child.prototype.constructor=Child;
Child.uber=Parent.prototype; }
extend(Cat,Animal); varcat1=newCat("大毛","黄色");
alert(cat1.species);//动物
Child.uber=Parent.prototype;
functionAnimal(){} Animal.prototype.species="动物";
functionextend2(Child,Parent){ varp=Parent.prototype;
varc=Child.prototype; for(variinp){
c[i]=p[i]; }
c.uber=p; }
extend2(Cat,Animal); varcat1=newCat("大毛","黄色");
alert(cat1.species);//动物
觉得可用,就经常来吧! 脚本宝典 欢迎评论哦! js脚本,巧夺天工,精雕玉琢。小宝典献丑了!
以上是脚本宝典为你收集整理的javascript代码实例教程-JavaScript面向对象编程深入分析全部内容,希望文章能够帮你解决javascript代码实例教程-JavaScript面向对象编程深入分析所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。