手写实现apply,call,bind和数组es5方法

本文介绍了JavaScript中的apply, call和bind方法的实现细节,特别是上下文(context)的处理。同时,详细讲解了数组的forEach, map, filter, find, every, some和reduce等ES5方法的工作原理和使用场景。" 89598589,5742150,SpringBoot应用的国际化实现,"['springboot2.1.4', '国际化', 'Thymeleaf']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值