1. apply方法(注意没有传context或者为undefined或null或string,number类型时的上下文context)
Function.prototype._apply=function(context){
if(context===undefined||context===null){
context=window;
}
if(typeof context==='string'||typeof context==='number'){
context={};
}
context.fn=this;
let args=arguments[1];
let res;
if(args){
res=context.fn(...args)
}else{
res=context.fn()
}
delete context.fn;
return res;
};
2. call(同样注意传进去的上下文即可,然后参数是从第二个开始)
Function.prototype._call=function(context){
if(context===undefined||context===null){
context=window;
}
if(typeof context==='string'||typeof context==='number'){
context={}
}
context.fn=this;
let args=[...arguments].slice(1);
let res=context.fn(...args);
delete context.fn;
return res;
}
3. bind(借助apply和call实现)
Function.prototype._bind=function(context){
let args=Array.prototype.slice.call(arguments,1);
let that=this;
return function(){
that.apply(context,args.concat(Array.prototype.slice.call(arguments)))
}
};
---------------------------------------------------------------
数组方法:
1. forEach(该方法是没有返回值的,为undefined)
Array.prototype._forEach=function(fn){
if(typeof fn!=='function'){
throw 'the arguments must be a function';
}
let arr=this;
if(!Array.isArray(arr)){
throw '只能对数组使用forEach方法'
}
for(let index=0;index<arr.length;index++){
fn(arr[index],index,arr);
}
};
2. map方法
Array.prototype._map=function(fn){
if(typeof fn!=='function'){
throw 'the arguments must be a function';
}
let arr=this;
let res=[];
if(!Array.isArray(arr)){
throw '只能对数组进行map方法'
}
for(let index=0;index<arr.length;index++){
res.push(fn(arr[index],index,arr))
}
return res;
};
3. filter方法(filter的回调返回的是true or false)
Array.prototype._filter=function(fn){
if(typeof fn!=='function'){
throw '参数必须为函数';
}
let arr=this;
if(!Array.isArray(arr)){
throw '只能对数组使用filter方法';
}
let res=[];
for(let index=0;index<arr.length;index++){
let s=fn(arr[index],index,arr);
s&&res.push(arr[index]);
}
return res;
};
4. find方法(找到第一个符合要求的并立即返回,找不到时返回undefined)
Array.prototype._find=function(fn){
if(typeof fn!=='function'){
throw '参数必须为函数';
}
let arr=this;
if(!Array.isArray(arr)){
throw '只能对数组使用filter方法';
}
for(let index=0;index<arr.length;index++){
let s=fn(arr[index],index,arr);
if(s){
return arr[index];
}
}
};
5. every方法
Array.prototype._every=function(fn){
if(typeof fn!=='function'){
throw '参数必须为函数';
}
let arr=this;
if(!Array.isArray(arr)){
throw '只能对数组使用filter方法';
}
for(let index=0;index<arr.length;index++){
let s=fn(arr[index],index,arr);
if(!s){
return false;
}
}
return true;
};
6. some方法
Array.prototype._some=function(fn){
if(typeof fn!=='function'){
throw '参数必须为函数';
}
let arr=this;
if(!Array.isArray(arr)){
throw '只能对数组使用filter方法';
}
for(let index=0;index<arr.length;index++){
let s=fn(arr[index],index,arr);
if(s){
return true;
}
}
return false;
};
7. reduce方法
Array.prototype._reduce=function(fn,initialValue){
if(typeof fn!=='function'){
throw '参数必须为函数';
}
let arr=this;
if(!Array.isArray(arr)){
throw '只能对数组使用filter方法';
}
let index=0;
if(!initialValue){
index=1;
initialValue=arr[0];
}
for(;index<arr.length;index++){
initialValue=fn(initialValue,arr[index],index,arr);
}
return initialValue;
};