javascript基于prototype实现类似OOP继承的方法
本文实例讲述了javascript基于prototype实现类似OOP继承的方法。分享给大家供大家参考,具体如下:
这里要说明的是,公有属性(使用this.修饰符)可以被覆盖,私有属性(使用var修饰符)不能被覆盖
子类不能访问父类的私有属性,父类的方法正常访问父类的私有变量。
functionVegetable(){
this.taste='delicious';
vara='I\'mVegetable\'a!'
this.fun1=function(){
alert('Vegetablefun1doing...');
}
this.fun3=function(){
alert(a);
}
}
functionCelery(){
vara='I\'mCelery\'a';
this.color='green';
this.taste='bad';
this.fun1a=function(){
alert('Celeryfun1doing...');
}
this.fun2=function(){
alert('Celeryfun2doing...');
}
this.fun4=function(){
alert(a);
}
}
Celery.prototype=newVegetable();
varstick=newCelery();
varpolymorphed=stick.taste;
//alert(polymorphed);
//alert(stick.color);
//stick.fun1();
//stick.fun2();
//stick.fun3();
stick.fun4();
希望本文所述对大家JavaScript程序设计有所帮助。
热门推荐
10 祝孙辈生日祝福语简短
11 恋人跨年祝福语大全简短
12 考大学敬酒简短祝福语
13 开工投产大吉祝福语 简短
14 偶然结婚祝福语朋友简短
15 感谢学长学姐祝福语简短
16 戏精的祝福语简短
17 我要结婚祝福语简短
18 婚前女儿生日祝福语简短