简单的单体??

<script>
var singleton = function() {
    var uniqueInstance;
    function constructor() {
        var id = '1';
        function add() {
            return id + '2';
        };

        function add1() {
            return '3';
        };

        return {
            name : 'name',
            method : function() {
                alert(this.name + add() + add1());
            }
        }
    }

    return {
        getInstance : function() {
            if(!uniqueInstance) {
                uniqueInstance = constructor();
            }
            return uniqueInstance;
        }
    }
}();

singleton.getInstance().method();
</script>

 

《javaScript设计模式》第五章观后感

 

posted @ 2012-03-01 09:12  Maxfong  阅读(202)  评论(0编辑  收藏  举报