Created
October 25, 2011 23:52
-
-
Save shepting/1314834 to your computer and use it in GitHub Desktop.
Module Pattern
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
<html> | |
<script type="text/javascript"> | |
// Test of the module pattern from Douglas Crockford's | |
// "Javascript: The Good Parts" pages 41 & 42 | |
console.log("Starting module test...") | |
var serial_maker = function () { | |
var prefix = ''; | |
var seq = 0; | |
return { | |
set_prefix: function (p) { | |
prefix = String(p); | |
}, | |
set_seq: function (s) { | |
seq = s; | |
}, | |
gensym: function () { | |
var ret_val = prefix + seq; | |
seq += 1; | |
return ret_val; | |
} | |
}; | |
}(); | |
// Setup details | |
serial_maker.set_prefix('Q'); | |
serial_maker.set_seq(1000); | |
// Test details | |
console.log("Unique:", serial_maker.gensym()); | |
console.log("Unique:", serial_maker.gensym()); | |
console.log("Unique:", serial_maker.gensym()); | |
</script> | |
<body> | |
<h3> Test Page</h3> | |
</body></html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment