为了账号安全,请及时绑定邮箱和手机立即绑定

js设置数组的最大长度

js设置数组的最大长度

富国沪深 2018-11-07 13:15:43
比如说设置一个数组最大长度为9,超过9就删除,要怎么做
查看完整描述

1 回答

?
侃侃尔雅

TA贡献1801条经验 获得超15个赞

//重写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;

}


查看完整回答
反对 回复 2018-12-12
  • 1 回答
  • 0 关注
  • 2463 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信