首先,中秋节要到了,祝我和朋友们中秋节快乐。
吃完饭突然想起了一次面试中还被问到的如何实现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 ;
}