zoukankan      html  css  js  c++  java
  • 安全模式的工厂模式

    var Factory = function(type,content){
            if (this instanceof Factory)
            {
                var s = new this[type](type,content);//运算符优先顺序
                return s;
            }else {
                return new Factory(type,content);
            }
        }
    
        Factory.prototype = {
            html:function(type,content){
                this.content = content;
                this.type = type;
                this.show = function(){
                    console.log(this.type+this.content);
                }
            },
            php:function(type,content){
                this.content = content;
                this.type = type;
                this.show = function(){
                    console.log(this.type+this.content);
                }
            }
        }
        var p1 = Factory('html','哪家强?');
        p1.show();
        var p2 = Factory('php','真的不错!');
        p2.show();

     三个不同功能的按钮

     <body>
      <input type='button' value='btn1' name='click'/>
      <input type='button' value='btn2' name='mouseover'/>
      <input type='button' value='btn3' name='dblclick'/>
      <div id='div1'></div>
      <script>
        var Factory = function(type,element){
            if (this instanceof Factory)
            {
                var s = new this[type](element);
                return s;
            }else {
                return new Factory(type,element);
            }
        }
        
        Factory.prototype = {
            click:function(element){
                element.addEventListener('click',function(){
                    oDiv.innerHTML = this.value;
                },false);
            },
            mouseover:function(element){
                element.addEventListener('mouseover',function(){
                    oDiv.innerHTML = this.value;
                },false);
            },
            dblclick:function(element){
                element.addEventListener('dblclick',function(){
                    oDiv.innerHTML = this.value;
                },false);
            }
        }
    
        var aBtn = document.getElementsByTagName('input'),
            oDiv = document.getElementById('div1');
        for (var i=0;i<aBtn.length ;i++ )
        {
            Factory(aBtn[i].name,aBtn[i]);
        }
        
      </script>
     </body>
  • 相关阅读:
    .net实现依赖注入
    Model Validation(模型验证)
    TCP应用
    Jquery框架
    Fiddler工具
    Oracle从11.2.0.2开始,数据库补丁包是一个完整安装包(转)
    java路径中的空格问题(转)
    分析Java的类加载器与ClassLoader(二):classpath与查找类字节码的顺序,分析ExtClassLoader与AppClassLoader的源码
    java中path和classpath
    velocity-1.7中vm文件的存放位置
  • 原文地址:https://www.cnblogs.com/jokes/p/9671141.html
Copyright © 2011-2022 走看看