zoukankan      html  css  js  c++  java
  • js原生设计模式——3简单工厂模式简单工厂模式封装简单对象

    1、Factory基本写法

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>简单工厂模式之一个对象有时候可以代替许多类</title>
        <script type="text/javascript">
        //简单工厂模式之一个对象有时候可以代替许多类
        //工厂模式
        function createBook(name,time,type){
            //封装一个对象,并对该对象拓展其属性和方法
            var o = new Object();
            //var o = {};
            o.name = name;
            o.time = time;
            o.type = type;
            o.getName = function(){
                console.log(this.name);//this指向当前对象o
            }
            return o;//将对象o返回出去
        }
        //测试用例
        var jsbook = createBook('js book',2015,'js');
        var cssbook = createBook('css book',2014,'css');
        jsbook.getName();
        cssbook.getName();
        //本例已经通过验证
        </script>
    </head>
    <body>
        
    </body>
    </html>

    2、Factory差异性写法

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>简单工厂模式之差异化写法——三种弹窗效果封装</title>
        <script type="text/javascript">
        //简单工厂模式之差异化写法——三种弹窗效果封装
        //工厂模式
        function createPop(type,text){
            //封装一个对象,并对该对象拓展其属性和方法
            var o = {};
            o.text = text;
            o.show = function(){
                console.log(this.text);//this指向当前对象o
                //show显示方法代码
            }
            if(type == 'alert'){
                alert(o.text);
            }else if(type == 'prompt'){
                prompt('提示',o.text);
            }else if(type == 'confirm'){
                confirm(o.text);
            }
            return o;//将对象o返回出去
        }
        //测试用例
        var userNameTip = createPop('confirm','用户名只能是12位以内字母下划线和数字组成');//创建即弹出
        userNameTip.show();
        //本例已经通过验证
        </script>
    </head>
    <body>
        
    </body>
    </html>

  • 相关阅读:
    Learning NFS/NIS 2nd 读书笔记-Chapter3 NIS Operation
    Linux Enterprise Cluster Notes Ch11 LVS Introduction Theory
    Linux Enterprise Cluster NOtes Ch7 A Sample HA config
    Linux Enterprise Cluster Notes Ch10 build a Linux cluster
    Linux Enterprise Cluster NOtes Ch8 Heartbeat配置和维护
    当被监控的应用发生问题时,heartbeat会failover么?
    Linux Enterprise Cluster NOtes Ch9 Stonith and IPFail
    Linux Enterprise Cluster NOtes Ch6 Heartbeat介绍和原理
    客户端不支持javascript怎么办
    js 返回对象|js返回多个值的方法|js如何返回多个值
  • 原文地址:https://www.cnblogs.com/koleyang/p/4936621.html
Copyright © 2011-2022 走看看