Array.prototype.join = function(arg) {
let result = this[0] || ''
const length = this.length
for (let i = 0; i< length; i++) {
result += arg + this[i]
}
return result
}
slice
Array.prototype.slice = function(begin, end) {
let result = []
begin = begin || 0
end = end || this.length
for (let i = begin; i < end; i++) {
result.push(this[i])
}
return result
}
forEach
Array.prototype.forEach = function(fn) {
for (let i = 0; i < this.length; i++) {
if (i in this) {
fn.call(undefined, this[i], i, this)
}
}
}
map
Array.prototype.map = function(fn) {
let result = []
for (let i = 0; i < this.length; i++) {
if (i in this) {
result[i] = fn.call(undefined, this[i], i, this)
}
}
return result
}
filter
Array.prototype.filter = function(fn) {
let result = []
let temp
for (let i = 0; i < this.length; i++) {
if (i in this) {
if (temp = fn.call(undefined, this[i], i, this)) {
result.push(temp)
}
}
}
return result
}
reduce
Array.prototype.reduce = function(fn, init) {
let result = init
for (let i = 0; i < this.length; i++) {
if (i in this) {
result = fn.call(undefined, result, this[i], i, this)
}
}
return result
}