javascript - js设置数组的最大长度
本文介绍了javascript - js设置数组的最大长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
比如说设置一个数组最大长度为9,超过9就删除,要怎么做
解决方案
//重写push方法
Array.prototype.push = function(o,capacity){
var start = this.length;
if(capacity && start>=capacity){
this.pop();
start--;
}
start = Math.max(start,0);
this.splice(start,0,o);
return this;
}
这篇关于javascript - js设置数组的最大长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文