Skip to content

Instantly share code, notes, and snippets.

@flipjs
Last active August 29, 2015 14:04
Show Gist options
  • Save flipjs/229cbc8b6ecf7f5f8009 to your computer and use it in GitHub Desktop.
Save flipjs/229cbc8b6ecf7f5f8009 to your computer and use it in GitHub Desktop.
IIFE + Augmentation
/*
Sample IIFE + Augmentation
Here I created a module counter,
and augmented it (added reset function).
*/
var counter = (function() {
var count = 0
return {
get: function() {
return count
},
set: function(val) {
count = val
},
increment: function(num) {
count += num || 1
}
}
})()
console.log(counter.get())
counter.set(4)
console.log(counter.get())
counter.increment()
console.log(counter.get())
counter.increment(10)
console.log(counter.get())
var counter = (function(oldNS) {
if (oldNS) {
oldNS.reset = function() {
oldNS.set(0)
}
}
return oldNS
})(counter)
counter.reset()
console.log(counter.get())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment