兀然想起一次面试被问实现ES5的bind方法,当时懵了,后面的回答一下子没了分寸。其实那些都是基础知识,今天就来实现一下Es5的几个重要的方法bind、forEach等,再纪念一下当时的囧态。 这是我参与8月更文挑战的第18天,活动详情查看:8月更文挑战
bind
bind方法解析:
- 每个函数实例都可调用此方法
- 可指定函数的执行上下文:
this
先前参数
和新参数
共同组成函数的参数
接着我们来确定实现方案:
- 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 ;
}
最后
感谢阅读!