手写es5 bind、forEach、find、reduce等方法

首先,中秋节要到了,祝我和朋友们中秋节快乐。
吃完饭突然想起了一次面试中还被问到的如何实现ES5的bind方法,当时一下子懵了,后面的回答一下子没了分寸…,,其实这些都是基础知识,相对于使用过的框架和库这些东西更能反映程序员的技术水平。
现在就来实现一下,纪念一下当时的囧态!

bind

bind方法解析:

  • 每个函数都有这个方法
  • 指定上下文
  • 先前参数新参数共同组成函数的参数

分析之后确定了几点:

  • bind函数挂在Function的原型上
  • 返回一个匿名函数
  • 将两种参数组合传递给bind函数
Function.prototype.bind = function(){
	//get context
	let context = arguments[0];
	//get function invoked in the future
	let fn = this;
	// get front args
	let frontArgs = Array.prototype.slice.call(arguments,1);
	return function(){
		let afterArgs = Array.prototype.slice.call(arguments,0);
		let finalArgs = frontArgs.concat(afterArgs);
		return fn.apply(context,finalArgs )
	}
}

forEach

数组的forEach解析:

  • 仅限数组使用
  • 回调参数为:每一项、索引、原数组
Array.prototype.forEach = function(fn){
	if(typeof fn !== "function"){
		throw "参数必须为函数"
	}
	//get array going to be iterated
	let arr = this;
	if(!Array.isArray(arr)){
		throw "只能对数组使用forEach方法"
	}
	
	for(let index=0;index<arr.length;index++){
		fn(arr[index],index,arr)
	}
}

filter

数组的filter解析:

  • 数组使用
  • 过滤掉回调函数返回值不为true的项
Array.prototype.filter = function(fn){
	if(typeof fn !== "function"){
		throw "参数必须为函数"
	}
	//get array going to be iterated
	let arr = this;
	if(!Array.isArray(arr)){
		throw "只能对数组使用forEach方法"
	}
	let result = [];
	for(let index=0;index<arr.length;index++){
		let invokedReturn = fn(arr[index],index,arr);
		if( invokedReturn ){
			result.push(arr[index])
		}		
	}
	return result;
}

find

数组的find解析:

  • 数组使用
  • 返回第一个回调函数返回true的项,没有则返回undefined
Array.prototype.find= function(fn){
	if(typeof fn !== "function"){
		throw "参数必须为函数"
	}
	//get array going to be iterated
	let arr = this;
	if(!Array.isArray(arr)){
		throw "只能对数组使用forEach方法"
	}

	for(let index=0;index<arr.length;index++){
		let invokedReturn = fn(arr[index],index,arr);
		if( invokedReturn ){
			return arr[index]
		}		
	}
	return void 0;
}

every

数组的every解析:

  • 数组使用
  • 所有回调函数返回值都为true时 结果为true,否则为false
Array.prototype.every= function(fn){
	if(typeof fn !== "function"){
		throw "参数必须为函数"
	}
	//get array going to be iterated
	let arr = this;
	if(!Array.isArray(arr)){
		throw "只能对数组使用forEach方法"
	}
	for(let index=0;index<arr.length;index++){
		let invokedReturn = fn(arr[index],index,arr);
		if( !invokedReturn ){
			return false;
		}		
	}
	return true;
}

some

数组的some解析:

  • 数组使用
  • 回调函数返回值一个为true 结果就为true, 否则为false
Array.prototype.some= function(fn){
	if(typeof fn !== "function"){
		throw "参数必须为函数"
	}
	//get array going to be iterated
	let arr = this;
	if(!Array.isArray(arr)){
		throw "只能对数组使用forEach方法"
	}
	for(let index=0;index<arr.length;index++){
		let invokedReturn = fn(arr[index],index,arr);
		if( invokedReturn ){
			return true;
		}		
	}
	return false;
}

reduce

数组的reduce解析:

  • 数组使用
  • 相比其他方法多了一个参数,上次调用的返回值
  • 最后一个回调函数的返回值为reduce的结果
  • 可以指定累积的初始值,不指定从第二项开始遍历
Array.prototype.reduce= function(fn,accumulator){
	
	if(typeof fn !== "function"){
		throw "参数必须为函数"
	}
	//get array going to be iterated
	let arr = this;
	if(!Array.isArray(arr)){
		throw "只能对数组使用forEach方法"
	}
	let index = 0;
	if(!accumulator){
		index = 1;
		accumulator = arr[0];
	}
	for(;index<arr.length;index++){
		let invokedReturn = fn(accumulator ,arr[index],index,arr);
		accumulator = invokedReturn ;
	}
	return accumulator ;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值