做个ˇ局外人

文章 评论 浏览 26

做个ˇ局外人 2022-05-04 13:54:14

/***

  • index: 删除的起始下标
  • num:删除个数
  • addlist: 新增的元素
  • returnItem :返回已删除元素
    */
Array.prototype._slice = function(index, num, ...addList) {
    let newArr = []
    let returnItem = []
    while(num > 0) {
        // 起始下标有可能传入负数
        if (index < 0) {
            index += this.length
        }
        // 删除当前项
        returnItem.push(this[index])
        delete this[index]
        index++
        num--
    }
    // 将老数组赋予新数组
    for(const item of arr) {
        if(item) newArr.push(item)
    }
    let length = newArr.length
    for(var i = 0; i< length;i++) {
        this[i] = newArr[i]
    }
    this.length = length
    // 新增元素
    if(addList && addList.length > 0) {
        for(const item of addList) {
            this.push(item)
        }
    }
    return returnItem
}

第 158 题:如何模拟实现 Array.prototype.splice

做个ˇ局外人 2022-05-04 13:26:41

@mqyqingfeng 想问一下题主,async/await 到底应该算是 ES7(2016) 还是 ES8 (2017) 呢?

网上有说是 ES7 的,也有说是 ES8 的 :-)

ES6 系列之我们来聊聊 Async

更多

推荐作者

櫻之舞

文章 0 评论 0

弥枳

文章 0 评论 0

m2429

文章 0 评论 0

野却迷人

文章 0 评论 0

我怀念的。

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文