JavaScript设计模式--单例模式

先上一小段代码,看看能不能看懂,三天后揭晓单例模式。。

$.tabControl = $.tabControl || {};

$.extend($.tabControl,{
    prop1:”abc”,//注意此处为单例类实例
    init:function($self,options){
          alert(this.prop1);//this是tabControl类实例,$self为控件本身
        this._privateMethod(“abc")
    },
    _privateMethod:function(param){
        //私有方法请用下划线开头
    }
});

$.fn.tab = function(options) {
    if (typeof options == 'string') {
        var fn = $.tabControl[options];
        if (!fn) {
            throw ("tabControl - No such method: " + options);
        }
        var args = $.makeArray(arguments).slice(1);
        args.unshift($(this));
        return fn.apply($.tabControl, args);
    }else{
        return this.each( function() {
            $(this).tab('init',options);
        });
    }
};
原文地址:https://www.cnblogs.com/jager/p/4868968.html