Javascript对象继承(原型继承法)

http://extjs.org.cn/node/407 (转)

 

 

<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>

你可能感兴趣的:(JavaScript)