这是一个有趣的问题,但是如果您的目标浏览器是最新的浏览器,不包括 < IE9,这应该可以工作:
var TreeHelper = function(items, limit, start) {
this.items_ = items || [];
this.limit = limit || 5;
this.start = start || 0;
}
TreeHelper.prototype = {
push: function(item) {
this.items_.push(item);
return this;
}
};
Object.defineProperty(TreeHelper.prototype, 'items', {
get: function() {
return this.items_.slice(this.start, this.limit);
}
});
var helper = new TreeHelper([item1, item2, item3, ...], 5, 0);
......
function_that_needs_m_Items(helper.items);