Last active
December 17, 2015 10:59
-
-
Save kovaldn/5599141 to your computer and use it in GitHub Desktop.
Javascript: patterns - object literal, module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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