首先声明,这里只写代码。至于方法的作用请自行查阅mdn文档。
1、Array.prototype.at()方法
Array.prototype.myAt = function(params = 0){
let temp = this,
index,
result,
if(typeof params == 'string'){
if(params.length == 0){
index = 0
}else{
if(isNaN(Number(params))){
index = 0
}else{
index = Number(params) == 0 ? 0 : Number(params) > 0 ? Number(params) : temp.length + Number(params)
}
}
}else if(typeof params == 'boolean'){
index = Number(params)
}else if(typeof params == 'number'){
index = params == 0 ? 0 : params > 0 ? params : temp.length + params
}else if(JSON.stringify(params) == "[]" || JSON.stringify(params) == '{}'){
index = 0
}
for(let i = 0,l = temp.length; i < l; i++){
if(i == index){
result = temp[i]
}
}
return result
}
2、Array.prototype.concat方法
Array.prototype.myConcat = function(...params){
if(params.length == 0)return this
reutrn [...this,...params].flat()
}
3、Array.prototype.every方法
Array.prototype.myEvery = function(fn,thisValue = window){
let temp = this
if(typeof fn != 'function'){
throw new Error(`${fn} is not a function`)
}
for(let i = 0,l = temp.length; i < l; i++){
if(!fn.call(thisValue,temp[i],i,temp)){
return false