Skip to content

Instantly share code, notes, and snippets.

@shepting
Created October 25, 2011 23:52
Show Gist options
  • Save shepting/1314834 to your computer and use it in GitHub Desktop.
Save shepting/1314834 to your computer and use it in GitHub Desktop.
Module Pattern
<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