js原生设计模式——3简单工厂模式js面向对象编程实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>oopObject——js面向对象编程实例</title>
    <script type="text/javascript">
    //篮球基类
    var Basketball = function(){
        this.info = '篮球在美国盛行';
    }
    Basketball.prototype = {
        getMember:function(){
            console.log('每个队伍需要5名队员');
        },
        getBallSize:function(){
            console.log('篮球很大');
        }
    }
    //足球基类
    var Football = function(){
        this.info = '足球在全世界都流行';
    }
    Football.prototype = {
        getMember:function(){
            console.log('每个队伍需要11名队员');
        },
        getBallSize:function(){
            console.log('足球很大');
        }
    }
    //网球基类
    var Tennis = function(){
        this.info = '每年都有很多网球公开赛';
    }
    Tennis.prototype = {
        getMember:function(){
            console.log('每个队伍需要1名队员');
        },
        getBallSize:function(){
            console.log('网球很小');
        }
    }
    //运动工厂类
    var SportsFactory = function(name){
        switch(name){
            case 'NBA':
              return new Basketball();
            case 'wordCup':
              return new Football();
            case 'FrenchOpen':
              return new Tennis();
        }
    }
    //测试用例
    var football = SportsFactory('wordCup');
    console.log(football);
    console.log(football.info);
    football.getMember();
    football.getBallSize();
    //本例已经通过验证
    </script>
</head>
<body>
    
</body>
</html>

原文地址:https://www.cnblogs.com/koleyang/p/4936612.html