javascript的模块开发方法;

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<script>
    //模块开发模式;
//    var someModule = (function(){
//        //TODO
//    }());
//    第一种返回方式;
//    var someModule = (function(){
//        var count = 0;
//        return {
//            addCount:function(){
//                return count++;
//            },
//            getCount: function(){
//                return count;
//            },
//            resetCount: function(){
//                console.log(count);
//                count = 0;
//            }
//        }
//    }());
//    第二种返回方式;
    var someModule = (function(){
        var count = 0;
            var addCount = function(){
                return count++;
            }
            var getCount = function(){
                return count;
            }
            var resetCount = function(){
                console.log(count);
                count = 0;
            }
        return {
            addCount: addCount,
            getCount: getCount,
            resetCount: resetCount

        }
    }());
    someModule;
    var a1 = someModule;
    console.log(someModule.addCount());
    console.log(someModule.addCount());
    console.log(someModule.addCount());
    console.log(a1.addCount());
    a1.resetCount();
    console.log(a1.getCount());
</script>
</body>
</html>

 

posted @ 2016-04-10 16:25  挥刀  阅读(139)  评论(0编辑  收藏  举报