Array.prototype.push

let arr = [1.2.3.4.5]
let arrLike = {0:1.1:2.length:2}
let obj = {}
/** * Array.prototype.push(element1,... ,elementN) * adds N elements to the end of the array and returns the array length * https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/push * push method is generic purpose. When used with call() or apply(), * can be applied to array-like objects. The push method uses the length attribute to determine where to start inserting the given value. * If length cannot be converted to a value, then the element is inserted with an index of 0, * including if length does not exist. Length is created when it does not exist. * /
Array.prototype.myPush = function() {
  var length = this.length ? this.length : (this.length = 0) && 0
  var index = 0
  while (index < arguments.length) {
    this[length] = arguments[index]
    ++index
    ++length
  }
  this.length = length
  return this.length
}
arr.myPush(1.2.3.4)
Array.prototype.myPush.call(obj,1.2, {})Array.prototype.myPush.call(arrLike,3.4, {})console.log(arr)
console.log(obj)
console.log(arrLike)
Copy the code

Array.prototype.pop

/** * array.prototype.pop () * Remove the element from the end of the Array and return it https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/pop * Elements removed from an array (undefined if the array is empty) * pop methods are intentionally generic. When used with call() or apply(), this method can be applied to array-like objects. * The pop method determines the position of the last element based on the length attribute. * If length is not included or cannot be converted to a value, set length to 0 and return undefined. * /
Array.prototype.myPop = function () {
  var length = this.length ? this.length : (this.length = 0) && 0
  if(length === 0)return
  var last = this[length-1]
  delete this[length - 1] -this.length
  return last
}
console.log(Array.prototype.myPop.call(arr))
console.log(Array.prototype.myPop.call(arrLike))
console.log(arr,obj,arrLike)
Copy the code

Array.prototype.shift

/** * array.prototype.shift () * Remove the element from the Array at index 0 and return the element * https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/shift * shift method to remove the index is 0 (the first element), and returns the removed element, * the index of the other elements subtracted by 1. If the value of the length attribute is 0 (length is 0), undefined is returned. * Shift is not limited to arrays: This method can be applied to array-like objects via the call or apply methods. * But it might not make sense to call this method on an object that does not have a length attribute (the last in a series of numeric attributes starting at 0). * * / 
Array.prototype.myShift = function(){
  var length = this.length ? this.length : 0
  if(length === 0)return
  var first = this[0]
  var index = 1
  while (index < this.length) {
    this[index - 1] = this[index]
    index ++
  }
  delete this[length - 1] -this.length
  return first
}
console.log(arr.myShift())
console.log(Array.prototype.myShift.call(obj))
console.log(Array.prototype.myShift.call(arrLike))
Copy the code

Array.prototype.unshift

/** * Array.prototype.unshift(element1,... ,elementN) * Inserts N elements into the array at index 0 and returns the length of the array * https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift * unshift Method inserts the given argument at the beginning of the array-like object that calls it. * Unshift is deliberately designed to be versatile; This method can be applied to array-like objects via the call or apply methods. * However, it may not make sense to call this method on an object that does not have a length attribute, which represents the last in a series of numeric attributes starting from 0. * * / 
Array.prototype.myUnshift = function() {
  if(!this.length)return
  var length = this.length
  var arglength = arguments.length
  this.length = length + arglength
  var index = length - 1
  while (index >= 0) {
    this[index + arglength] = this[index]
    --index
  }
  index = arglength - 1
  while (index >= 0) {
    this[index] = arguments[index]
    --index
  }
  return this.length
}
console.log(arr.myUnshift(0.1.3.4.5))
console.log(Array.prototype.myUnshift.call(obj,0.1.3.4.5))
console.log(Array.prototype.myUnshift.call(arrLike,0.1.3.4.5))
Copy the code

Array.prototype.slice

/** * array.prototype.slice (begin,end) * the array.prototype.slice (begin,end) * method returns a new Array object. This object is a shallow copy of the Array determined by begin and end (not including end). The original array will not be changed. * https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/slice * slice don't modify the original array, Returns only a shallow copy of the elements in the original array. * If the element is an object reference (not an actual object), Slice will copy the object reference to the new array. * Both object references refer to the same object. If the referenced object changes, then this element in the new and original arrays also changes. * For strings, numbers, and Booleans (not String, Number, or Boolean objects), Slice will copy them into the new array. * Modifying the string or number or Boolean value in another array will not affect the other array. * * / 
Array.prototype.mySlice = function(begin,end) {
  if(!this.length)return
  var arr = []
  var index = 0
  begin = typeof begin === 'number' ? 
        begin < 0 ? this.length + begin 
        : begin 
        : 0
  end =  typeof end === 'number' ? 
        end > this.length ? this.length 
        : end 
        : this.length
  while (begin < end) {
    arr[index] = this[begin]
    begin++
    index++
  }
  return arr
}
console.log(arr.mySlice(-4))
console.log(Array.prototype.mySlice.call(obj,0.2))
console.log(Array.prototype.mySlice.call(arrLike,0.1))
Copy the code

Array.prototype.concat

/** * Array.prototype.concat(item1,... The,itemN) * method returns a new array object that is a shallow copy of the original array determined by begin and end (excluding end). The original array will not be changed. * https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/concat * concat method creates a new array, It consists of the elements of the object being called, * each argument in the order of the element of that argument (if the argument is an array) or the argument itself (if it is not an array). It does not recurse into nested array parameters. * The concat method does not change this or any array provided as an argument, but instead returns a shallow copy * */ 
Array.prototype.myConcat = function() {
  if(!arguments.length)return
  var arr = []
  var index = 0
  while (index<this.length) {
    arr[index] = this[index]
    ++index
  }
  var i = 0
  while (i < arguments.length) {
    var el = arguments[i]
    if(el instanceof Array) {
      if(el.length){
        var j = 0
        while (j < el.length) {
          arr[index] = el[j]
          ++index
          ++j
        }
      }
    }else{
      arr[index] = el
      ++index
    }
    ++i
  }
  return arr
}
console.log(arr.myConcat(1[2.3], [4[5[6], [7]]]))
console.log(arr.concat(1[2.3], [4[5[6], [7]]]))
console.log(arr)
Copy the code

Array.prototype.splice

/** * Array.prototype.splice(start,deleteCount,item1,... The,itemN) * method modifies an array by removing or replacing existing elements and returns the modified contents as an array. This method changes the array. * https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/splice * If the number of elements added to the array does not equal the number of elements removed, the length of the array changes accordingly. * * / 
Array.prototype.mySplice = function(start,deleteCount) {
  if(!this.length)return
  var arr = []
  /** * If the length of the array is exceeded, add the contents from the end of the array; * If the value is negative, the number of digits starting from the end of the array (counting from -1); * If the absolute value of a negative number is greater than the length of the array, it starts at the 0th bit. * /
  start = typeof start === 'number' ?
          start > this.length ? this.length 
        : start < 0 ? this.length + start < 0 ? 0 
        : this.length + start 
        : start 
        : 0
  /** * If deleteCount is greater than the total number of elements after start, then all elements after start will be deleted (including the start bit). * If deleteCount is omitted, it is equivalent to array.length-start. If deleteCount is omitted or its value is greater than or equal to array. length-start (that is, if it is greater than or equal to the number of elements after start), then all elements of the array after start are deleted. * If deleteCount is 0 or negative, the element is not removed. In this case, at least one new element should be added. * /
  deleteCount = typeof deleteCount === 'number' ? 
              deleteCount < 0 ? 0 
            : deleteCount > this.length - start ? this.length - start 
            : deleteCount : deleteCount === undefined ? this.length - start 
            : 0
  // Remove the remaining parameters after removing the first two
  var args = arguments.length > 2 ? Array.prototype.mySlice.call(arguments.2) : []
  var argLength = args.length

  // Record the starting position
  var oIndex = start
  // The number to be added or reduced
  var moveLength = argLength - deleteCount
  // Need to delete to the specified index
  var delIndex = deleteCount + start
  // Add to the specified table below
  var addIndex = argLength + start
  var index = 0
  // Delete [...start,... delIndex,...
  while (start < delIndex) {
    arr[index] = this[start]
    this[start] = null
    ++start
    ++index
  }
  if(moveLength > 0) {// If the array is not sufficient to insert, open a new position
    var i = this.length - 1
    this.length += moveLength
    while (i >= oIndex) {
      this[i+moveLength] = this[i]
      --i
    }
  }else{
    // There is still space left after the insert
    var i = this.length
    if(start < this.length){
      while (start < i) {
        this[start+moveLength] = this[start]
        ++start
      }
    }
    this.length += moveLength
  }
  var i = 0
  // Insert new item1... itemN
  while (oIndex < addIndex) {
    this[oIndex] = args[i]
    ++i
    ++oIndex
  }
  return arr
}
console.log(arrLike)
console.log(Array.prototype.mySplice.call(arrLike,1.1))
console.log(arrLike)
console.log(arr.mySplice())
console.log(arr)
Copy the code

Array.prototype.reduce

/** * array.prototype.reduce (callback,initialValue) * reduce() executes a reducer function (ascending) that you provide for each element in the Array, and aggregates the results into a single return value. * https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce * callback perform the function of the each value in the array, Accumulator is an accumulator that accumulates the return value of the callback. It is the cumulative value, or initialValue, returned when the callback was last called (see below). CurrentValue: The element being processed in the array. * currentIndex Optional: Index of the current element being processed in the array. If initialValue is provided, the starting index number is 0, otherwise it is 1. InitialValue Is optional. InitialValue is the value of the first argument when the first callback is called. If no initial value is provided, the first element in the array is used. Calling Reduce on an empty array with no initial value will report an error. * /
Array.prototype.myReduce = function(callback){
  var arr = this.mySlice()
  var len = arr.length
  var index = 0
  var initialValue
  if(arguments.length >= 2) {// Take the default value if there is one
    initialValue = arguments[1]}else{
    // If there is no default value, take the first index with a value, and process the sparse array. If the first item has no value, the starting index goes back
    while(index < len && ! (arr[index]in arr)) {
      ++index
    }
    if(index >= len) return
    initialValue = arr[index++]
  }
  while (index < len) {
    if(arr[index] in arr){
      // The value exists before coming in
      initialValue = callback.call(null, initialValue, arr[index], index, arr)
    }
    ++index
  }
  return initialValue
}
var sum = [0.1.2.3.4].myReduce(function(accumulator, currentValue, currentIndex, array){
  console.log(accumulator, currentValue, currentIndex, array)
  return accumulator + currentValue;
}); / / 10
var sum = [, 1.3,,].myReduce(function(accumulator, currentValue, currentIndex, array){
  console.log(accumulator, currentValue, currentIndex, array)
  return accumulator + currentValue;
}); / / 4
Copy the code

Array.prototype.reduceRight

/ * * * Array. Prototype. ReduceRight (callback, the initialValue) * reduceRight () method on each element of the Array to execute a reducer function provided by you (in descending order), Summarize its results into a single return value. * https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/reduceRight * callback An accumulator is an accumulator that accumulates the return value of the callback. It is the cumulative value, or initialValue, returned when the callback was last called (see below). CurrentValue: The element being processed in the array. * currentIndex Optional: Index of the current element being processed in the array. If initialValue is provided, the starting index number is 0, otherwise it is 1. * Array Optional: An array of calls to reduceRight() * initialValue Optional * As the value of the first argument when the callback function is first called. If no initial value is provided, the first element in the array is used. Calling reduceRight on an empty array with no initial value will report an error. * /
Array.prototype.myReduceRight = function(callback){
  var arr = this.mySlice()
  var len = arr.length-1
  var index = len
  var initialValue
  if(arguments.length >= 2) {// Take the default value if there is one
    initialValue = arguments[1]}else{
    // If there is no default value, take the first index with a value and process the sparse array. If the last item has no value, the starting index moves forward
    while (index >= 0 && !(arr[index] in arr)) {
      --index
    }
    if(index <= 0) return
    initialValue = arr[index--]
  }
  while (index >= 0) {
    if(arr[index] in arr){
      // The value exists before coming in
      initialValue = callback.call(null, initialValue, arr[index], index, arr)
    }
    index--
  }
  return initialValue
}
var sum = [0.1.2.3.4].myReduceRight(function(accumulator, currentValue, currentIndex, array){
  console.log(accumulator, currentValue, currentIndex, array)
  return accumulator + currentValue;
},2); / / 12
var sum = [, 1.3,,].myReduceRight(function(accumulator, currentValue, currentIndex, array){
  console.log(accumulator, currentValue, currentIndex, array)
  return accumulator + currentValue;
},2); / / 6
Copy the code

Array.prototype.forEach

/** * array.prototype.foreach (callback,context) * forEach() executes the provided function once forEach element of the Array. Generate new array elements * https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach * callback function, Use three arguments: * currentValue * Callback the current element being processed in the array. * index Optional * callback The index of the current element being processed in the array. * array Optional * Callback The array in which the map method was called. * thisArg Optional * This value to use when executing the callback function. * /
Array.prototype.myForEach = function(callback){
  var len = this.length
  var index = 0
  var context = arguments[1] | |this
  while (index < len) {
    callback.call(context, this[index], index, this)
    index++
  }
}
[1.2.3.4.5].forEach(function(current, index, arr) {
  console.log(current, index, arr, this.a)
},{a:1})
[1.2.3.4.5].myForEach(function(current, index, arr){
  console.log(current, index, arr, this.a)
},{a:1})
Copy the code

Array.prototype.map

/** * array.prototype.map (callback,context) * map() method creates a new Array with the result of each element in the Array calling one of the provided functions. Generate new array elements * https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/map * callback function, Use three arguments: * currentValue * Callback the current element being processed in the array. * index Optional * callback The index of the current element being processed in the array. * array Optional * Callback The array in which the map method was called. * thisArg Optional * This value to use when executing the callback function. * /
Array.prototype.myMap = function(callback){
  var arr = []
  var len = this.length
  var index = 0
  var context = arguments[1] | |this
  while (index < len) {
    arr.myPush(callback.call(context, this[index], index, this))
    index++
  }
  return arr
}
console.log([1.2.3.4.5].map(function(current, index, arr) {
  console.log(current, index, arr)
  return index + this.a
},{a:1})) / / [1, 2, 3, 4, 5]
console.log([1.2.3.4.5].myMap(function(current, index, arr) {
  console.log(current, index, arr)
  return index + this.a
},{a:1})) / / [1, 2, 3, 4, 5]
Copy the code

Array.prototype.filter

/** * array.prototype.filter (callback,context) * filter() method creates a new Array containing all the elements that passed the test implemented by the provided function. Generate new array elements * https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/filter * callback function, Use three arguments: * currentValue * Callback the current element being processed in the array. * index Optional * callback The index of the current element being processed in the array. * Array Optional * The array in which the callback filter method is called. * thisArg Optional * This value to use when executing the callback function. * /
Array.prototype.myFilter = function(callback){
  var arr = []
  var len = this.length
  var index = 0
  var context = arguments[1] | |this
  while (index < len) {
    var el = this[index]
    callback.call(context, el, index, this) && arr.myPush(el)
    index++
  }
  return arr
}
console.log([1.2.3.4.5].filter(function(current, index, arr) {
  console.log(current, index, arr)
  return index > this.a
},{a:1}))
console.log([1.2.3.4.5].myFilter(function(current, index, arr) {
  console.log(current, index, arr)
  return index > this.a
},{a:1}))/** * array.prototype.filter (callback,context) * filter() method creates a new Array containing all the elements that passed the test implemented by the provided function. Generate new array elements * https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/filter * callback function, Use three arguments: * currentValue * Callback the current element being processed in the array. * index Optional * callback The index of the current element being processed in the array. * Array Optional * The array in which the callback filter method is called. * thisArg Optional * This value to use when executing the callback function. * /
Array.prototype.myFilter = function(callback){
  var arr = []
  var len = this.length
  var index = 0
  var context = arguments[1] | |this
  while (index < len) {
    var el = this[index]
    callback.call(context, el, index, this) && arr.myPush(el)
    index++
  }
  return arr
}
console.log([1.2.3.4.5].filter(function(current, index, arr) {
  console.log(current, index, arr)
  return index > this.a
},{a:1}))/ / / three, four, five
console.log([1.2.3.4.5].myFilter(function(current, index, arr) {
  console.log(current, index, arr)
  return index > this.a
},{a:1}))/ / / three, four, five
Copy the code

Array.prototype.every

/** * array.prototype. every(callback,context) * every() tests whether all elements of the Array pass the specified function test. Generate new array elements * https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/every * callback function, Use three arguments: * currentValue * Callback the current element being processed in the array. * index Optional * callback The index of the current element being processed in the array. * array Optional * callback Every Specifies the array in which the method is called. * thisArg Optional * This value to use when executing the callback function. * /
Array.prototype.myEvery = function(callback){
  var every = true
  var len = this.length
  var index = 0
  var context = arguments[1] | |this
  while (index < len) {
    if(! callback.call(context,this[index], index, this)) {
      every = false 
      break 
    }
    index++
  }
  return every
}
console.log([1.2.3.4.5].every(function(current, index, arr) {
  console.log(current, index, arr)
  return current > this.a
},{a:0})) // true
console.log([1.2.3.4.5].myEvery(function(current, index, arr) {
  console.log(current, index, arr)
  return current > this.a
},{a:0})) // true
Copy the code

Array.prototype.some

/** * array.prototype. some(callback,context) * some() tests whether at least one element passes the test implemented by the provided function. Generate new array elements * https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/some * callback function, Use three arguments: * currentValue * Callback the current element being processed in the array. * index Optional * callback The index of the current element being processed in the array. * array Optional * callback Some Specifies the array in which the method was called. * thisArg Optional * This value to use when executing the callback function. * /
Array.prototype.mySome = function(callback){
  var every = false
  var len = this.length
  var index = 0
  var context = arguments[1] | |this
  while (index < len) {
    if(callback.call(context, this[index], index, this)) {
      every = true 
      break 
    }
    index++
  }
  return every
}
console.log([1.2.3.4.5].some(function(current, index, arr) {
  console.log(current, index, arr)
  return current > this.a
},{a:10})) // false
console.log([1.2.3.4.5].mySome(function(current, index, arr) {
  console.log(current, index, arr)
  return current > this.a
},{a:10})) // false
Copy the code

Array.prototype.find

/** * arr.find(callback[, thisArg]) returns the value of the first element in the array that satisfies the provided test function. Otherwise, return undefined. * https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/find * callback function to be executed by the * on each item in the array, Take three arguments: * element * The element currently traversed. * Index Optional * The index being traversed. * Array Optional * Array itself. * thisArg optional * The object used for this when performing the callback. * /
Array.prototype.myFind = function(callback,context) {
  context = context || window
  var len = this.length
  var i = 0
  while (i < len) {
    if(callback.call(context,this[i],i,this))
      return this[i]
    i++
  }
  return undefined
}
console.log([4.6.8.12].myFind(function(item) {
  return item + this.a > 10}, {a:5})); / / 6
console.log([4.6.8.12].find(function(item) {
  return item + this.a > 10}, {a:5})); / / 6
Copy the code

Array.prototype.findIndex

/** * arr.findIndex(callback[, thisArg]) returns the index of the first element in the array that satisfies the provided test function. Otherwise, return 1 callback * * https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex * A function that executes on each element in an array and takes three arguments: * element * The element currently traversed. * Index Optional * The index being traversed. * Array Optional * Array itself. * thisArg optional * The object used for this when performing the callback. * /
Array.prototype.myFindIndex = function(callback,context) {
  context = context || window
  var len = this.length
  var i = 0
  while (i < len) {
    if(callback.call(context,this[i],i,this))
      return i
    i++
  }
  return -1
}
console.log([4.6.8.12].myFindIndex(function(item) {
  return item + this.a > 10}, {a:5})); / / 1
console.log([4.6.8.12].findIndex(function(item) {
  return item + this.a > 10}, {a:5})); / / 1
Copy the code

Array.prototype.join

The /** * array.prototype.join (separator) * join() method concatenates all the elements of an Array (or an array-like object) into a string and returns the string. If the array has only one item, the item is returned without a delimiter. * https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/join * separator * Specifies a string to separate each element of the array. Convert the delimiter to a string if needed. * If () is omitted, the array elements are separated by commas. The default is ",". If separator is an empty string (""), then there are no characters between all the elements. * /
Array.prototype.myJoin = function(separator){
  var separator = typeof separator === 'string' ? separator : ', '
  var len = this.length
  var str = ' '
  if(! len)return str
  var index = 1
  str = this[0]?this[0].toString() : ' ' 
  while (index < len) {
    str += separator + (this[index] ? this[index].toString() : ' ')
    index++
  }
  return str
}
console.log([1.null{}, [],/ 2 /].myJoin(', ') = = = [1.null{}, [],/ 2 /].join(', ')) //true

Copy the code

Array.prototype.reverse

The /** * array.prototype.reverse () * reverse() method reverses the positions of the elements in the Array and returns the Array. This method changes the array. * https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse * way to reverse the position of the element in the array, And return a reference to the array. * /
Array.prototype.myReverse = function(){
  if(!this.length) return this
  var len = this.length - 1
  var index = 0
  var mid = Math.floor(this.length / 2)
  while (index < mid) {
    var lastIndex = len-index
    var tem = this[index]
    var last = this[lastIndex]
    varindexEmpty = ! (indexin this)
    varlastIndexEmpty = ! (lastIndexin this)

    if(lastIndexEmpty){
      delete this[index]
    }else{
      this[index] = last
    }
    if(indexEmpty){
      delete this[lastIndex]
    }else{
      this[len-index] = tem
    }
    index++
  }
  return this
}
var arr1 = [1.2.3.4.5]
var arr2 = [1.2.3.4.5]
arr1.myReverse()
console.log(arr1) //[5, empty, 4, empty, 3, empty, 2, 1]
arr2.reverse()
console.log(arr2) //[5, empty, 4, empty, 3, empty, 2, 1]
Copy the code

Array.prototype.sort

The /** * sort() method sorts the elements of the array using the in-place algorithm and returns the array. The sorting algorithm is now stable. The default sort order is based on string Unicode code points. CompareFunction optional * * https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/sort * A function used to specify an order. If omitted, the elements are sorted by the Unicode loci of each character in the string to which they are converted. * firstEl * The first element used for comparison. * secondEl * The second element for comparison. * /
/** **@param {*} Arr Array to be sorted *@param {*} Low starting point *@param {*} High end *@param {*} Cb comparison function */
function quickSort(arr,low,high,cb) {
  if(low<high){
    var mid = partition(arr,low,high,cb)
    quickSort(arr,low,mid-1,cb)
    quickSort(arr,mid+1,high,cb)
  }
  return arr
}
/** partition function */
function partition(arr,low,high,cb) {
  var poivt = arr[low]
  while (low<high) {
    while (low<high && cb(arr[high],poivt) >= 0 ) {
      high--
    }
    arr[low] = arr[high]
    while (low<high && cb(arr[low],poivt) <= 0 ) {
      low++
    }
    arr[high] = arr[low]
  }
  arr[low] = poivt
  return low
}
Array.prototype.mySort = function(cb) {
  return quickSort(this.0.this.length-1,cb)
}


var arr1 = [3.5.5, -1.65.6.41.2.51.11.52.8]
var arr2 = [3.5.5, -1.65.6.41.2.51.11.52.8]
function fcb(a,b) {
  return a - b
}
console.log(arr1.mySort(fcb)) //[-1, 2, 3, 5, 5, 6, 8, 11, 41, 51, 52, 65]
console.log(arr2.sort(fcb)) //[-1, 2, 3, 5, 5, 6, 8, 11, 41, 51, 52, 65]
Copy the code

Array.prototype.indexOf

The /** * indexOf() method returns the first index in the array where a given element can be found, or -1 if none exists. * https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf * searchElement * Element to find * fromIndex * where the search begins. If the index is greater than or equal to the length of the array, it means that the array is not looked up, and -1 is returned. * If the index value provided in the argument is a negative value, it is used as an offset at the end of the array, * that is, -1 means the search starts with the last element, -2 means the search starts with the penultimate element, and so on. Note: * If the index value is a negative value, the lookup order is not changed. * The lookup order is still to query the array backwards. If the offset index is still less than 0, the entire array will be queried. The default value is 0. */
Array.prototype.myIndexOf = function(search,fromIndex){
  fromIndex = fromIndex ? typeof fromIndex === 'number' ? fromIndex 
                : typeof fromIndex === 'string' ? (fromIndex-=0) && fromIndex === fromIndex ? fromIndex 
                : 0 : 0 : 0
  var index = -1
  var len = this.length
  var i = fromIndex < 0 ? len + fromIndex : fromIndex
  while (i < len) {
    if(search == this[i]){
      index = i
      break
    }
    i++
  }
  return index
}
console.log(arr1.myIndexOf(5,{}) == arr1.indexOf(5{})),//true
console.log(arr1.myIndexOf(5,[]) == arr1.indexOf(5[])),//true
console.log(arr1.myIndexOf(5[1]) == arr1.indexOf(5[1])) //true
console.log(arr1.myIndexOf(5.'1') == arr1.indexOf(5.'1')) //true
console.log(arr1.myIndexOf(5.'1e') == arr1.indexOf(5.'1e')) //true
console.log(arr1.myIndexOf(5.true) == arr1.indexOf(5.true)) //true
console.log(arr1.myIndexOf(5.NaN) == arr1.indexOf(5.NaN)) //true
console.log(arr1.myIndexOf(5, -1) == arr1.indexOf(5, -1)) //true
console.log(arr1.myIndexOf(5, -5) == arr1.indexOf(5, -5)) //true
Copy the code

Array.prototype.lastIndexOf

The /** * lastIndexOf() method returns the first index in the array where a given element can be found, or -1 if none exists. * https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf * searchElement * The element to look for * fromIndex * works backwards from this location. The default is the length of the array minus 1, which means the entire array is searched. * If the value is greater than or equal to the length of the array, the entire array is looked up. * If it is negative, consider it an offset forward from the end of the array. * Even if the value is negative, the array will still be looked up backwards. * If the value is negative and its absolute value is greater than the array length, the method returns -1, * that is, the array is not looked up. * /
Array.prototype.myLastIndexOf = function(search,fromIndex){
  fromIndex = fromIndex ? typeof fromIndex === 'number' ? fromIndex 
              : (fromIndex-=0) && fromIndex === fromIndex ? fromIndex 
              : 0 
              : 0
  var index = -1
  var i = fromIndex < 0 ? fromIndex + this.length > 0 ? fromIndex + this.length : 0 : fromIndex > this.length ? this.length : fromIndex
  while (i > 0) {
    if(search == this[i]){
      index = i
      break
    }
    i--
  }
  return index
}
console.log(arr1.myLastIndexOf(5,{}) == arr1.lastIndexOf(5{})),//true
console.log(arr1.myLastIndexOf(5,[]) == arr1.lastIndexOf(5[])),//true
console.log(arr1.myLastIndexOf(5[1]) == arr1.lastIndexOf(5[1])) //true
console.log(arr1.myLastIndexOf(5.'1') == arr1.lastIndexOf(5.'1')) //true
console.log(arr1.myLastIndexOf(5.'1e') == arr1.lastIndexOf(5.'1e')) //true
console.log(arr1.myLastIndexOf(5.true) == arr1.lastIndexOf(5.true)) //true
console.log(arr1.myLastIndexOf(5.NaN) == arr1.lastIndexOf(5.NaN)) //true
console.log(arr1.myLastIndexOf(5, -1) == arr1.lastIndexOf(5, -1)) //true
console.log(arr1.myLastIndexOf(5, -5) == arr1.lastIndexOf(5, -5)) //true
Copy the code

Array.prototype.from

/** * array. from(arrayLike[, mapFn[, thisArg]]) create a new Array instance from an array-like or iterable. * https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/from * arrayLike * A pseudo-array object or iterable that you want to convert to an array. MapFn (optional) * If specified, this callback is performed for each element in the new array. * thisArg (optional) * Optional argument, this object when the callback function mapFn is executed. * /
Array.prototype.myFrom = function(arrayLike,mapFn,context) {
  context = context || window
  mapFn = mapFn || function(item){return item}
  var arr = []
  if(arrayLike.forEach){
    arrayLike.forEach((value) = >{
      arr.push(mapFn.call(context,value))
    })
  }else{
    var length = arrayLike.length
    var i = 0
    while (i<length) {
      arr.push(mapFn.call(context,arrayLike[i]))
      i++
    }
  }
  return arr
}
console.log(Array.prototype.myFrom(arrLike))
console.log(Array.prototype.myFrom(set))
console.log(Array.prototype.myFrom(map))
Copy the code

Array.prototype.of

The /** * array. of(element0[, element1[,...[, elementN]]]) method creates a new Array instance with a variable number of arguments, regardless of the number or type of the arguments. * https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/of * elementN * any parameters, Will become elements in the returned array, in order. * /
Array.prototype.myOf = function() {
  var len = arguments.length
  var arr =[]
  arr.length = len
  var i = 0
  while (i < len) {
    arr[i] = arguments[i]
    i++
  }
  return arr
  // return Array.prototype.mySlice.call(arguments)
  // return Array.prototype.myFrom.call(null,arguments)
}
console.log(Array.prototype.myOf(1.2.3))
console.log(Array.prototype.myOf(undefined))
console.log(Array.prototype.myOf(1))
Copy the code

Array.prototype.copyWithin

/** * array.copyWithin (target[, start[, end]]) method shallowly copies a part of the Array into another position in the same Array and returns it without changing its size. Target * https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/copyWithin * * 0 as the basal indexes, Copy the sequence to that location. If it is negative, target is computed from the end. * If the target is greater than or equal to arr.length, no copy will occur. If target is after start, the copied sequence will be modified to match arr.length. * start * 0 is the base index, starting to copy the starting position of the element. If it is negative, start will be computed from the end. * If start is ignored, copyWithin will start from 0. * end * 0 is the base index and starts at the end of the copy element. CopyWithin will copy to that location, but not to the end location. If it is negative, end will be computed from the end. * If end is ignored, the copyWithin method will copy all the way to the end of the array (default is arr.length). * /
Array.prototype.myCopyWithin = function(target,start,end) {
  var len = this.length
  target = target < 0 ? Math.abs(target) > len ? len : len + target : target > len ? len : target
  start = typeof start === 'number' ? start < 0 ? Math.abs(start) > len ? len : len + start : start > len ? len : start : 0
  end = typeof end === 'number' ? end < 0 ? Math.abs(end) > len ? len : len + end : end > len ? len : end : len
  var oTarget = target
  var offset = end - start
  var arr = Array.prototype.mySlice.call(this)
  while (target < len && (target-oTarget) < offset && start < end) {
    if(!this[start])break
    this[target] = arr[start]
    start++
    target++
  }
  return this
}
console.log([1.2.3.4.5].myCopyWithin(-2)); // [1, 2, 3, 1, 2]
console.log([1.2.3.4.5].copyWithin(-2)); // [1, 2, 3, 1, 2]
console.log([1.2.3.4.5].myCopyWithin(0.3)); // [4, 5, 3, 4, 5]
console.log([1.2.3.4.5].copyWithin(0.3)); // [4, 5, 3, 4, 5]
console.log([1.2.3.4.5].myCopyWithin(0.3.4));// [4, 2, 3, 4, 2]
console.log([1.2.3.4.5].copyWithin(0.3.4));// [4, 5, 3, 4, 5]
console.log([1.2.3.4.5].myCopyWithin(-2, -3, -1)); // [1, 2, 3, 3, 4]
console.log([1.2.3.4.5].copyWithin(-2, -3, -1)); // [1, 2, 3, 3, 4]
console.log([1.2.3.4.5].myCopyWithin(3.2.4)); // [1, 2, 3, 3, 4]
console.log([1.2.3.4.5].copyWithin(3.2.4)); // [1, 2, 3, 3, 4]
console.log([].myCopyWithin.call({length: 5.3: 1}, 0.3)); // {0: 1, 3: 1, length: 5}
console.log([].copyWithin.call({length: 5.3: 1}, 0.3)); // {0: 1, 3: 1, length: 5}
console.log([].myCopyWithin.call(new Int32Array([1.2.3.4.5]), 0.3.4));// Int32Array [4, 2, 3, 4, 5]
console.log([].copyWithin.call(new Int32Array([1.2.3.4.5]), 0.3.4));// Int32Array [4, 2, 3, 4, 5]
Copy the code

Array.prototype.fill

/** * array.fill (callback[, thisArg]) uses a fixed value to fill all elements of an Array from start index to end index. Terminating indexes are not included. * https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/fill * value * used to populate the value of the element array. * start Optional * Start index. The default value is 0. * end Optional * Terminates the index. The default is this.length. * /
Array.prototype.myFill = function(value,start,end) {
  var len = this.length
  start = typeof start === 'number' ? start < 0 ? Math.abs(start) > len ? len : len + start : start > len ? len : start : 0
  end = typeof end === 'number' ? end < 0 ? Math.abs(end) > len ? len : len + end : end > len ? len : end : len
  while (start < end) {
    this[start] = value
    start++
  }
  return this
}
console.log([1.2.3].myFill(4))               / / (4, 4, 4]
console.log([1.2.3].myFill(4.1))            / / [1, 4, 4]
console.log([1.2.3].myFill(4.1.2))         / / [1, 4, 3)
console.log([1.2.3].myFill(4.1.1))         / / [1, 2, 3]
console.log([1.2.3].myFill(4.3.3))         / / [1, 2, 3]
console.log([1.2.3].myFill(4, -3, -2))       / / [4, 2, 3]
console.log([1.2.3].myFill(4.NaN.NaN))     / / [1, 2, 3]
console.log([1.2.3].myFill(4.3.5))         / / [1, 2, 3]
console.log(Array(3).myFill(4))                / / (4, 4, 4]
console.log(Array.prototype.myFill.call({ length: 3 }, 4))  // {0: 4, 1: 4, 2: 4, length: 3}
Copy the code

Array.prototype.includes

/** * array.prototype. includes(valueToFind[, fromIndex]) returns true if an Array contains a specified value, false if not. * https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/includes * valueToFind * The value of the element to look for. * fromIndex Optional * Start the search for valueToFind from the fromIndex index. The default is 0. * If the value is negative, the search starts with array. Length + fromIndex's index in ascending order (even if the search starts with the fromIndex's absolute value index and then searches backwards). * /
Array.prototype.myIncludes = function(valueToFind,findIndex) {
  var len = this.length
  findIndex = typeof findIndex === 'number' ? findIndex < 0 ? Math.abs(findIndex) > len ? len : len + findIndex : findIndex > len ? len : findIndex : 0
  while (findIndex < len) {
    var now = this[findIndex]
    if(valueToFind === now)return true
    if(valueToFind ! == valueToFind && now ! == now)return true
    findIndex++
  }
  return false
}
console.log([1.2.3].myIncludes(2))     // true
console.log([1.2.3].myIncludes(4))     // false
console.log([1.2.3].myIncludes(3.3))  // false
console.log([1.2.3].myIncludes(3, -1)) // true
console.log([1.2.NaN].myIncludes(NaN)) // true
Copy the code

Array.prototype.keys

/** * Array.prototype.keys() returns an Array Iterator containing each key in the Array. * https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/keys */
Array.prototype.myKeys = function() {
  if(!typeof this= = ='object')return 
  var arr = null
  var length = this.length
  if(! length){ arr = []for (const key in this) {
      if (this.hasOwnProperty(key)) {
        arr.push(key)
      }
    }
  }
  var len = this.length || arr.length
  var nextIndex = 0
  return {
    //[Symbol. Iterator] We need to add this attribute to an object so that a Symbol can be used for... Of traversed objects
    [Symbol.iterator]: function(){
      return {
        next:function(){
          return nextIndex < len ? {value: length ? nextIndex++ : arr[nextIndex++], done:false}, {done:true}
        }
      }
    }
  }
}
var a = ["a"."b"."c"].myKeys()
var b = Array.prototype.myKeys.call({0:1.1:2.length:2})
var c = Array.prototype.myKeys.call({a:1.b:2})
for (const value of a) {
  console.log(value) / / 0 1 2
}
for (const value of b) {
  console.log(value) / / 0 to 1
}
for (const value of c) {
  console.log(value) // a b
}
Copy the code

Array.prototype.values

/** * array.prototype.values () returns a new Array Iterator containing the value of each index of the Array. * https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/values */
Array.prototype.myValues = function() {
  if(!typeof this= = ='object')return 
  var arr = this
  if(!this.length){
    arr = []
    for (const key in this) {
      if (this.hasOwnProperty(key)) {
        arr.push(this[key])
      }
    }
  }
  var len = this.length || arr.length
  var nextIndex = 0
  return {
    //[Symbol. Iterator] We need to add this attribute to an object so that a Symbol can be used for... Of traversed objects
    [Symbol.iterator]: function(){
      return {
        next:function(){
          return nextIndex < len ? {value: arr[nextIndex++], done:false}, {done:true}
        }
      }
    }
  }
}
var a = ["a".'b'."c"].myValues()
var b = Array.prototype.myValues.call({0:1.1:2.length:2})
var c = Array.prototype.myValues.call({a:1.b:2})
for (const value of a) {
  console.log(value) // a b c
}
for (const value of b) {
  console.log(value) / / 1. 2
}
for (const value of c) {
  console.log(value) / / 1. 2
}
Copy the code

Array.prototype.entries

The array.prototype.entries () method returns a new Array Iterator containing key/value pairs for each index in the Array. * https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/entries */
Array.prototype.myEntries = function() {
  if(!typeof this= = ='object')return 
  var arr = this
  var len = this.length || arr.length
  var nextIndex = 0
  return {
    //[Symbol. Iterator] We need to add this attribute to an object so that a Symbol can be used for... Of traversed objects
    [Symbol.iterator]: function(){
      return {
        next:function(){
          return nextIndex < len ? {value:[nextIndex,arr[nextIndex++]], done:false}, {done:true}
        }
      }
    }
  }
}
var a = ["a".'b'."c"].myEntries()
var b = Array.prototype.myEntries.call({0:1.1:2.length:2})
for (const value of a) {
  console.log(value) // [0,"a"] [0, "b"] [0, "c"]
}
for (const value of b) {
  console.log(value) // [0, 1] [0, 2]
}
Copy the code