比如說設(shè)置一個數(shù)組最大長度為9,超過9就刪除,要怎么做
js設(shè)置數(shù)組的最大長度
富國滬深
2018-11-07 13:15:43
TA貢獻1801條經(jīng)驗 獲得超16個贊
//重寫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;
}
舉報