1 var Pagination = (function() { 2 var _default_limit = 1; 3 var isArray = Array.isArray || function(arr) { 4 return {}.toString.call(arr) == '[object Array]'; 5 }; 6 7 var isInt = function(x) { 8 var type = typeof x; 9 return type !== null && (type === 'number');10 }11 12 function _Pagination(list, limit) {13 if (!(this instanceof Pagination)) {14 return new Pagination(list, limit || _default_limit);15 }16 if (!isArray(list)) throw new Error("list must be array");17 this.list = list;18 this.limit = limit;19 this.total = Math.ceil(list.length / limit);20 return this;21 }22 23 _Pagination.prototype.page = function(pagenum) {24 if (!isInt(pagenum)) throw new Error("pagenum must be number");25 if (pagenum < 1 || pagenum > this.total) {26 throw new Error("pagenum must be in range[" + 1 + ", " + this.total + "]");27 }28 var start = this.limit * (pagenum - 1);29 return this.list.slice(start, start + this.limit);30 };31 32 _Pagination.prototype.toString = function() {33 var result = [];34 for (var i = 1; i <= this.total; i++) {35 result.push("[" + this.page(i).join(',') + "]");36 }37 return result.join(',');38 }39 return _Pagination;40 41 })();42 43 var pagination = Pagination([1, 32, 43, 4, 15, 6, 7, 8, 9, 10,34],2);44 console.log(pagination+"")