zoukankan      html  css  js  c++  java
  • 单例模式

    单例模式

    了解什么是模块

    ```
    function CoolModule() {
        var something = "cool";
        var another = [1, 2, 3];
        function doSomething() { console.log( something );
        }
        function doAnother() {
        console.log( another.join( " ! " ) );
        }
        return {
        doSomething: doSomething, doAnother: doAnother
        }; 
        }
        var foo = CoolModule(); foo.doSomething(); // cool
        foo.doAnother(); // 1 ! 2 ! 3
    ```
    

    这个模式在 JavaScript 中被称为模块。最常见的实现模块模式的方法通常被称为模块暴露, 这里展示的是其变体。

    单例模式:当只需要一个实例时,可以对这个模式进行简单的 改进来实现单例模式。

    ```
    var foo = (function CoolModule() { var something = "cool";
    var another = [1, 2, 3];
    function doSomething() { console.log( something );
    }
    function doAnother() {
    console.log( another.join( " ! " ) );
    }
    return {
    doSomething: doSomething, doAnother: doAnother
    }; })();
    foo.doSomething(); // cool foo.doAnother(); // 1 ! 2 ! 3
    ```
    
    • 利用自执行函数,立即调用这个函数并将返回值直接赋值给单例的模块实例标识符 foo。
  • 相关阅读:
    datagridview 保存为excel输出
    将excel导入到datatable
    Json 转换为c#数组
    css 没有图片则隐藏或者显示默认图片
    模拟Get请求
    根据多个字符分隔字符串
    DbHelperSQL
    数据库相关
    json对象
    ajax滚动条懒加载
  • 原文地址:https://www.cnblogs.com/angle-xiu/p/14025457.html
Copyright © 2011-2022 走看看