zoukankan      html  css  js  c++  java
  • 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>

  • 相关阅读:
    NX 8.5 License Server Firewall Setting
    Cisco ASA intra-interface routing
    How to configure windows machine to allow file sharing with dns alias (CNAME)
    Install unifi controller on CentOS
    Android图片加载框架Glide用法
    判断app是否已启动
    SharedPreferences的一个工具类适合的数据类型包括String、Integer、Boolean、Float、Long
    android获取缓存大小和清除缓存
    对字符串进行MD5加密工具类
    android代码设置RelativeLayout的高度
  • 原文地址:https://www.cnblogs.com/koleyang/p/4936612.html
Copyright © 2011-2022 走看看