javascript基于prototype实现类似OOP继承的方法
本文实例讲述了javascript基于prototype实现类似OOP继承的方法。分享给大家供大家参考,具体如下:
这里要说明的是,公有属性(使用this.修饰符)可以被覆盖,私有属性(使用var 修饰符)不能被覆盖
子类不能访问父类的私有属性,父类的方法正常访问父类的私有变量。
function Vegetable(){ this.taste='delicious'; var a = 'I\'m Vegetable\'a!' this.fun1 = function(){ alert('Vegetable fun1 doing...'); } this.fun3 = function(){ alert(a); } } function Celery(){ var a = 'I\'m Celery\' a'; this.color = 'green'; this.taste = 'bad'; this.fun1a = function(){ alert('Celeryfun1 doing...'); } this.fun2 = function(){ alert('Celery fun2 doing...'); } this.fun4 = function(){ alert(a); } } Celery.prototype = new Vegetable(); var stick = new Celery(); var polymorphed = stick.taste; //alert(polymorphed); //alert(stick.color); //stick.fun1(); //stick.fun2(); //stick.fun3(); stick.fun4();
希望本文所述对大家JavaScript程序设计有所帮助。
相关推荐
kaixinfelix 2020-07-27
waitzkj 2020-06-20
fgleeldq 2020-06-14
xinhao 2020-06-09
也许会有hui 2020-05-03
D先生 2020-05-09
hoooooolyhu 2020-04-23
iflreey 2020-03-05
taiyanghua 2020-02-14
kanpiaoxue 2020-01-31
牧码人 2020-01-25
shawroad 2020-01-07
bluetears 2019-12-26
loviezhang 2019-12-15
Burgesszheng 2019-12-13
liusarazhang 2019-12-06