数组原型方法实现

215 阅读1分钟

实现数组原型的方法

forEach

Array.prototype.forEach=function(callback,thisArg){
  if(this==null){
    throw new TypeError('this is null of defined')
  }
  if(typeof callback !=='function'){
    throw new TypeError(callback+'is not a function')
  }
  const O=Object(this)
  const len=O.length>>>0
  let k=0
  while(k<len){
    if(k in O){
      callback.call(thisArg,O[k],k,O)
    }
    k++
  }
}

map

Array.prototype.map=function(callback,thisArg){
  if(this==null){
    throw new TypeError('this is null of defined')
  }
  if(typeof callback !=='function'){
    throw new TypeError(callback+'is not a function')
  }
  const O=Object(this)
  const len=O.length>>>0
  let k=0
  let res=[]
  while(k<len){
    if(k in O){
      res[k]=callback.call(thisArg,O[k],k,O)
    }
    k++
  }
  return res
}

filter

Array.prototype.filter=function(callback,thisArg){
  if(this==null){
    throw new TypeError('this is null of defined')
  }
  if(typeof callback !=='function'){
    throw new TypeError(callback+'is not a function')
  }
  const O=Object(this)
  const len=O.length>>>0
  let k=0
  let res=[]
  while(k<len){
    if(k in O){
      if(callback.call(thisArg,O[k],k,O)){
        res.push(O[k])
      }
    }
    k++
  }
  return res
}

some

Array.prototype.some=function(callback,thisArg){
  if(this==null){
    throw new TypeError('this is null of defined')
  }
  if(typeof callback !=='function'){
    throw new TypeError(callback+'is not a function')
  }
  const O=Object(this)
  const len=O.length>>>0
  let k=0
  while(k<len){
    if(k in O){
      if(callback.call(thisArg,O[k],k,O)){
        return true
      }
    }
    k++
  }
  return false
}

every

Array.prototype.every=function(callback,thisArg){
  if(this==null){
    throw new TypeError('this is null of defined')
  }
  if(typeof callback !=='function'){
    throw new TypeError(callback+'is not a function')
  }
  const O=Object(this)
  const len=O.length>>>0
  let k=0
  while(k<len){
    if(k in O){
      if(!callback.call(thisArg,O[k],k,O)){
        return false
      }
    }
    k++
  }
  return true
}

find

Array.prototype.find=function(callback,thisArg){
  if(this==null){
    throw new TypeError('this is null of defined')
  }
  if(typeof callback !=='function'){
    throw new TypeError(callback+'is not a function')
  }
  const O=Object(this)
  const len=O.length>>>0
  let k=0
  while(k<len){
    if(k in O){
      if(callback.call(thisArg,O[k],k,O)){
        return O[k]
      }
    }
    k++
  }
  return undefined
}

reduce

Array.prototype.reduce=function(callback,initialValue){
  if(this==null){
    throw new TypeError('this is null of defined')
  }
  if(typeof callback !=='function'){
    throw new TypeError(callback+'is not a function')
  }
  const O=Object(this)
  const len=O.length>>>0
  let k=0,acc
  if(arguments.length>1){
    acc=initialValue
  }else{
    while(k<len&&!(k in O)){
      k++
    }
    if(k>len){
      throw new TypeError( 'Reduce of empty array with no initial value' )
    }
    acc=O[k++]
  }
  while(k<len){
    if(k in O){
      acc=callback(acc,O[k],k,O)
    }
    k++
  }
  return acc
}