var pad = function() { var tbl = []; return function(num, n) { var len = n-num.toString().length; if (len <= 0) return num; if (!tbl[len]) tbl[len] = (new Array(len+1)).join('0'); return tbl[len] + num; } }(); pad(10,4); //返回:0010 pad(1000,4); //返回:1000