Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lynsei/345146f61f221a5d441091f642199c49 to your computer and use it in GitHub Desktop.
Save lynsei/345146f61f221a5d441091f642199c49 to your computer and use it in GitHub Desktop.
[my favorite things, these are a few of them] #iffe #js
// IIFE: Immediately invoked, functional expr
var counter = (function(){
var i = 0;
return {
get: function(){
return i;
},
set: function( val ){
i = val;
},
increment: function() {
return ++i;
}
};})();
// 'counter' is an object with properties, which in this case happen to be// methods.
counter.get(); // 0
counter.set( 3 );
counter.increment(); // 4
counter.increment(); // 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment