虽然有时候遍历数组只须要 for
循环则足矣,可是若是 API 利用得当,每每能更大程度的提升代码的可读性,减小心智负担~javascript
判断数组中是否至少有一个项经过了预设的条件,结果返回 boolean
。前端
callback
:执行的回调函数,用于条件判断。thisArg
:执行函数的 this
指针。判断数组 [ 1, 2, 3, 5, 6, 7 ]
中是否存在偶数java
const target = [ 1, 2, 3, 5, 6, 7 ] if (target.some(a => a % 2 === 0)) { // do something }
Array.prototype.some = function(fn, thisArg) { // 异常处理 if (this == null) { throw new TypeError('Cannot read property of null or undefined.') } if (typeof fn !== 'function') { throw new TypeError(`${fn} must be a function.`) } // 须要用 Object 包装一次 this const O = Object(this) const len = O.length || 0 for (let i = 0; i < len; i++) { if (i in O) { if (fn.call(thisArg, O[i], i, O)) { return true } } } return false }
判断数组中是否所有项都经过了预设的条件,结果返回 boolean
。数组
callback
:执行的回调函数,用于条件判断。thisArg
:执行函数的 this
指针。判断数组 [ 1, 2, 3, 5, 6, 7 ]
是否每一个数都是偶数。微信
const target = [ 1, 2, 3, 5, 6, 7 ] if (target.every((num) => num % 2 === 0)) { // do something }
Array.prototype.every = function(fn, thisArg) { // 异常处理 if (this == null) { throw TypeError('Cannot read property of null or undefined') } if (typeof fn !== 'function') { throw TypeError(`${fn} is not a function`) } // 从新包装一次 this const O = Object(this) const len = O.length || 0 for (let i = 0; i < len; i++) { if (i in O) { if (!fn.call(thisArg, O[i], i, O)) { return false } } } return true }
浅拷贝数组,能够指定开始和结束下标来对数组某段作拷贝。若是不添加任何参数,那么会直接拷贝整个数组。函数
begin
(可选参数): 从这个下标开始拷贝,若是为负数,则表示从倒数第 begin
开始拷贝。end
(可选参数): 从这个下标结束拷贝,若是为负数,则表示从倒数第 end
结束拷贝。拷贝数组 [ 1, 2, 3, 5, 6, 7 ]
到另一个数组。this
const target = [ 1, 2, 3, 5, 6, 7 ] const temp = target.slice()
Array.prototype.slice = function(begin, end) { if (this == null) { throw new TypeError('cannot read property of null or undefined') } // 若是 end 没有传就默认截到数组末尾 end = (typeof end !== 'undefined') ? end : this.length const cloned = [] const len = this.length let i = 0 // 处理下 begin 参数 let start = begin || 0 start = (start >= 0) ? start : Math.max(0, len + start) let upTo = (typeof end == 'number') ? Math.min(end, len) : len if (end < 0) { upTo = len + end } // 计算 upTo 到 start 之间的差值 let size = upTo - start // 若是 size > 0 就计算 // 再拷贝到 cloned 数组中 if (size > 0) { for (i = 0; i < size; i++) { cloned[i] = this[start + i] } } return cloned };
对数组中的每一个元素执行一个由您提供的reducer函数,将其结果汇总为单个返回值。prototype
callback
指针
reduce
的数组)initValue
计算数组 [ 1, 2, 3, 5, 6, 7 ]
的和。code
const target = [ 1, 2, 3, 5, 6, 7 ] const sum = target.reduce((prev, curr) => prev + curr)
Array.prototype.reduce = function(fn, initValue) { // 异常判断 if (this == null) { throw new TypeError('Cannot read property of null or undefined') } if (typeof fn !== 'function') { throw new TypeError(`${fn} must be a function`) } const O = Object(this) const len = O.length || 0 let i = 0 let k = 0 let accumulator = initValue // 遍历并拿到结果 while (i < len) { if (i in O) { if (!accumulator) { accumulator = O[i] } else { accumulator = fn.call(this, accumulator, O[i], i, O) } } else { k++ } i++ } // 空数组异常判断 if (k >= len) { throw new TypeError('Reduce of empty array with no initial value') } return accumulator }
建立一个新数组,其结果是该数组中的每一个元素调用一次提供的函数后的返回值。
callback
currentValue
index
array
thisArg
将数组 [ 1, 2, 3, 5, 6, 7 ]
转化成对象数组,格式为 [{val: 1}, ...]
const target = [ 1, 2, 3, 5, 6, 7 ] const objArr = target.map((num) => { return { val: num } })
Array.prototype.map = function(fn, thisArg) { // 异常判断 if (this == null) { throw new TypeError('Cannot read property of null or undefined.') } if (typeof fn !== 'function') { throw new TypeError(`${fn} is not a function.`) } const O = Object(this) const len = O.length || 0 const res = [] // 遍历并拿到结果 for (let i = 0; i < len; i++) { if (i in O) { res.push(fn.call(thisArg, O[i], i, O)) } } return res }
看完上面这些 Polyfill
以后,咱们能够找到一些规律,以便遇到没见过的 Polyfill
也能写出个大概:
this
指针和传入的回调函数作异常判断this
用 Object
从新包装一层搜索「tony老师的前端补习班」关注个人微信公众号,那么就能够第一时间收到个人最新文章。