原型继承是如何实现的,我们一起来看看下面的例子:
<html xmlns="http://www.w3.org/1999/xhtml"><head> <title></title> <script type="text/javascript"> function dw(s) { document.write(s + "<br />"); } //警察 function PoliceMan() { var m_lifeEnergy = 100; this.getLifeEnergy = function () { return m_lifeEnergy; } this.Shot = function () { m_lifeEnergy -= 1; } this.Repair = function() { m_lifeEnergy += 1; } } //超级警察 function SuperPoliceMan() { this.Flight = function() { } } SuperPoliceMan.prototype = new PoliceMan(); //创建一个超级警察 var pm = new SuperPoliceMan(); //显示当前生命值 dw(pm.getLifeEnergy()); //中枪生命值减1 pm.Shot(); //显示当前生命值 dw(pm.getLifeEnergy()); //飞离地面 pm.Flight(); //自我修复 pm.Repair(); //显示当前生命值 dw(pm.getLifeEnergy()); </script></head><body></body></html>
出处:http://extjs.org.cn
最新评论