javascript设计模式-模块模式(module pattern)

模块模式(module pattern

var someModule = ( function () {
    var privateVar = 5;
    var privateMethod = function () {
        return 'Private Test' ; 
    };
    return {
        publicVar: 10,
        publicMethod: function () {
            return ' Followed By Public Test ' ;
        },
        getData: function () {
            return privateMethod() + this. publicMethod() + privateVar; 
        }
    }
})();
someModule.getData();

暴露模块模式(revealing module pattern)

var myRevealingModule = ( function(){
    var name = 'John Smith' ;
    var age = 40;
    function updatePerson(){
        name = 'John Smith Updated' ;
    }
    function setPerson () {
        name = 'John Smith Set' ;
    }
    function getPerson () {
        return name;
    }
    return {
        set: setPerson,
        get: getPerson
    };
}());
myRevealingModule. get();

扩展阅读: JavaScript-Module-Pattern-In-Depth

你可能感兴趣的:(javascript设计模式-模块模式(module pattern))