Too Long; Didn't Read
Every time you click on a button a square is added and a message with the number of squares is displayed. The code tells you more than a thousand words about the Publish/Subscribe pattern. Here I'm using a Publish-Subscribe Implementation (Module) which is in charge of executing every function that has been subscribed to the event. The only work the handler will do is increment theby one and publish theevent. Here you'll find a really good explanation about The Observer Pattern with JavaScript.