js手写数组push(),unshift(),pop(),shift(),map()方法

目录

1、push()

2、unshift()

3、pop()

4、shift()

5、map()


1、push()

        Array.prototype.push=function(){
            for(let i=0;i

2、unshift()

  Array.prototype.unshift = function () {
            const args = [...arguments]
            const oldArray = [...this]
            for (let i = 0; i < args.length; i++) {
                this[i] = args[i]
            }
            for (let j = 0; j < oldArray.length; j++) {
                this[this.length] = oldArray[j]
            }
            return this.length
        }
        const arr = [1, 2, 3]
        console.log(arr.unshift(6, 5, 4))

3、pop()

Array.prototype.pop=function(){
    if(this.length===0)return undefined
    const lastItem=this[this.length-1]
    this.length--
    return lastItem
}
const arr=[1,3,4]
console.log(arr.pop())

4、shift()

Array.prototype.shift=function(){
    if(this.length===0)return undefined
    const firstItem=this[0]
    for(let i=0;i

5、map()

Array.prototype.map=function(fn){
    const newArr=[]
    for(let i=0;i

你可能感兴趣的:(js笔记,javascript,前端,typescript)