Last active
August 29, 2015 14:04
-
-
Save flipjs/229cbc8b6ecf7f5f8009 to your computer and use it in GitHub Desktop.
IIFE + Augmentation
This file contains 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
/* | |
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