JavaScript设计模式

// 创建窗体 单例
        var CreateWindow = (function () {

            // 设置属性
            function Singleton(option) {

                //设置option变量为接收的参数或者为空(如果没有提供的话)
                var option = option || {};
            }

            // 设置方法
            Singleton.prototype = {
                constructor: Singleton,

                fn: function() {
                    console.log('fn');
                }
            };

            //实例容器
            var instance;

            var _static = {
                name: 'CreateWindow',

                //获取实例的方法
                //返回Singleton的实例
                getInstance: function (args) {
                    if (instance === undefined) {
                        instance = new Singleton(args);
                    }
                    return instance;
                }
            };
            return _static;
        })();

        var cw = CreateWindow.getInstance({});
        cw.fn();
原文地址:https://www.cnblogs.com/sorrowx/p/6972251.html