JavaScript Design Pattern

in javascript •  7 years ago 

Method

(function() {
    //  private 변수들과 함수들 선언
    
    return {
        //  public 변수들과 함수들을 선언
    }
    
})();

Revealing Method

var Exposer = (function() {
    var privateVariable = 10;
    
    var privateMethod = function() {
        privateVariable++;
    }
    
    var methodToExpose = function() {
        
    }
    
    var otherMethodIWantToExpose = function() {
        privateMethod();
    }
    
    return {
        first: methodToExpose,
        second: otherMethodIWantToExpose
    };
})();

Exposer.first();
Exposer.second();
Exposer.methodToExpose;

Singleton

var singleton = (function() {
  var instance;
    function initiate() {
      return {
        a:function(){},
        b:function(){}
      };
    };
    
    return {
        getInstance: function() {
            if (!instance) {
                instance = initiate();
            }
            return instance;
        }    
    };
})();

var first = singleton.getInstance();
var second = singleton.getInstance();

console.log(first == second); //true

Prototype

var car = function() {
    this.numWheels = 4;
    this.manufacturer = '현대';
    this.make = '그랜져';
}

car.prototype.go = function() {
    
}

car.prototype.stop = function() {
    
}

Revealing Prototype

var car = function() {
    this.numWheels = 4;
    this.manufacturer = '현대';
    this.make = '그랜져';
}

car.prototype = function() {
    var go = function() {
        
    };
    
    var stop = function() {
        
    }
    
    return {
        pressBrakePedal: stop,
        pressGasPedal; go
    }
}();
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Congratulations @znxkznxk1030! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

Are you a DrugWars early adopter? Benvenuto in famiglia!
Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Congratulations @znxkznxk1030! You received a personal award!

Happy Steem Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

Downvote challenge - Add up to 3 funny badges to your board
Vote for @Steemitboard as a witness to get one more award and increased upvotes!