Skip to content

Instantly share code, notes, and snippets.

@kovaldn
Last active December 17, 2015 10:59
Show Gist options
  • Save kovaldn/5599141 to your computer and use it in GitHub Desktop.
Save kovaldn/5599141 to your computer and use it in GitHub Desktop.
Javascript: patterns - object literal, module
/*
* Architecture - Object Literal
* Advantages:
* - Easer to navigate and discuss
* - Profilers give you actual names to work with
* - You can execute these from firebug console
* - You can write unit tests against them
*/
(function() {
var app = {
initialize : function () {
this.modules();
this.setUpListeners();
},
modules: function () {
},
setUpListeners: function () {
$('form').click($.proxy(this.submitForm, this));
},
submitForm: function () {
// some actions here
// this === app ( $.proxy help with it)
}
}
app.initialize();
}());
/*
* Module in javascript
*/
var myModule = (function (){
var counter = 0;
function increment (){
counter += 1;
return counter;
}
function decrement (){
counter -= 1;
return counter;
}
function reset (){
counter = 0;
return counter;
}
// return an object
return {
increment : increment,
decrement : decrement,
reset : reset
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment