js面向对象1

对于js面向对象写一个比较初级的程序 模拟一个动物园的唱歌比赛

数据类
    /// <summary>
    /// 动物父类
    /// </summary>
    function animal()
    {
        /// <summary>
        /// 姓名
        /// </summary>
        var name;


    }

function bird() {
    this.sing = function () {
        return 100;
    }
}

bird.prototype = new animal();


function mouse() {
    this.sing = function () {
        return 50;
    }
}

mouse.prototype = new animal();


操作类
function singmatch() {
    this.work = function work(la) {
        var d = 0;
        var win = null;
        for ( i in la) {
            var d1 = la[i].sing();
            if (d1 > d) {
                d = d1;
                win = la[i];
            }
        }
        return "winner is " + win.name;
    }

  
}

表现类
<script type="text/javascript" src="animal.js"></script> 
<script type="text/javascript" src="bird.js"></script> 
<script type="text/javascript" src="mouse.js"></script> 
<script type="text/javascript" src="singmatch.js"></script> 

<script>
    var a = new singmatch();
    var bird1 = new bird();
    bird1.name = "bird1";

    var mouse1 = new mouse();
    mouse1.name = "mouse1";

    var list = [bird1, mouse1];

   
    document.write( a.work(list));
</script>

  

  

原文地址:https://www.cnblogs.com/frog2008/p/2326184.html