forEach 原理
使用 for 实现
const array = ['稚名真白', '樱岛麻衣', '蝶祈']
Array.prototype._forEach = function (callback) {
for (let i = 0; i < this.length; i++){
callback(this[i], i, this)
}
}
array._forEach((value, index, array) => {
console.log(value, index, array)
})
使用 for in 实现
const array = ['稚名真白', '樱岛麻衣', '蝶祈']
Array.prototype._forEach = function (callback) {
for (let key in this) {
if ('NaN' === (+key + '')) return
callback(this[key], key, this)
}
}
array._forEach((value, index, array) => {
console.log(value, index, array)
})
filter 原理
使用 for 实现
const array = ['稚名真白', '樱岛麻衣', '蝶祈']
Array.prototype._filter = function (callback) {
const res = []
for (let i = 0; i < this.length; i++){
callback(this[i], i, this) && res.push(this[i])
}
return res
}
const ゆずりはいのり = array._filter((value, index, array) => {
return value === '蝶祈'
})
console.log(ゆずりはいのり)
使用 for in 实现
const array = ['稚名真白', '樱岛麻衣', '蝶祈']
Array.prototype._filter = function (callback) {
const res = []
for (const key in this) {
if ('NaN' === (+key + '')) return res
callback(this[key], key, this) && res.push(this[key])
}
}
const ゆずりはいのり = array._filter((value, index, array) => {
return value === '蝶祈'
})
console.log(ゆずりはいのり)
map 原理
使用 for 实现
const array = ['稚名真白', '樱岛麻衣', '蝶祈']
Array.prototype._map = function (callback) {
const res = []
for (let i = 0; i < this.length; i++){
res.push(callback(this[i], i, this))
}
return res
}
const newArray = array._map((value, index, array) => {
return index + '-' + value
})
console.log(newArray)
使用 for in 实现
const array = ['稚名真白', '樱岛麻衣', '蝶祈']
Array.prototype._map = function (callback) {
const res = []
for (const key in this) {
if ('NaN' === (+key + '')) return res
res.push(callback(this[key], key, this))
}
}
const newArray = array._map((value, index, array) => {
return index + '-' + value
})
console.log(newArray)
reduce
使用 for 实现
const array = [2, 4, 6, 7, 8]
Array.prototype._reduce = function (callback, initValue) {
if (Object.prototype.toString.call(callback) !== '[object Function]') {
throw new Error('callback must be function')
}
let initIndex = arguments.length == 1 ? 1 : 0;
let accumulator = initIndex ? this[0] : initValue
for (let i = initIndex; i < this.length; i++) accumulator = callback(accumulator, this[i], i, this)
return accumulator
}
const res = array._reduce((accumulator, currentValue, currentIndex, array) => {
console.log(accumulator)
return accumulator += currentValue ** 2
}, 10)
console.log(res)
使用 for in 实现
const array = [2, 4, 6, 7, 8]
Array.prototype._reduce = function (callback, initValue) {
if (Object.prototype.toString.call(callback) !== '[object Function]') {
throw new Error('callback must be function')
}
let initIndex = arguments.length === 1 ? 1 : 0;
let accumulator = initIndex ? this[0] : initValue
for (const key in this) {
if('NaN' === +key + '') return accumulator
accumulator = callback(accumulator, this[key], key, this)
}
}
const res = array._reduce((accumulator, currentValue, currentIndex, array) => {
console.log(accumulator)
return accumulator += currentValue ** 2
}, 10)
console.log(res)