我试图重现Douglas Crockford所着的"Javascript:The Good Parts"一书中的一些代码.我们的想法是使用闭包进行对象封装,避免使用Javascript固有的全局变量.
var serial_maker = function ( ) {
// Produce an object that produces unique strings. A
// unique string is made up of two parts: a prefix
// and a sequence number. The object comes with
// methods for setting the prefix and sequence
// number, and a gensym method that produces unique
// strings.
var prefix = '';
var seq = 0;
return {
set_prefix: function (p) {
prefix = String(p);
},
set_seq: function (s) …Run Code Online (Sandbox Code Playgroud)