I was asked to implement the bind method of ES5 in an interview. In fact, those are the basic knowledge, today we will implement Es5 several important methods bind, forEach, and so on, and remember the embarrassing state at that time.
This is the 18th day of my participation in the August Genwen Challenge.More challenges in August
bind
Bind:
- This method can be called for each function instance
- The execution context of a function can be specified:
this
Previous parameter
andA new parameter
The parameters that together make up a function
Then we will determine the implementation scheme:
- Bind hangs on top of the prototype of Function
- Returns an anonymous function
- Pass both parameter combinations to the bind function
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 )
}
}
Copy the code
forEach
ForEach; forEach;
- Arrays only
- The callback arguments are: per item, index, raw array
Array.prototype.forEach = function(fn){
if(typeoffn ! = ="function") {throw "Parameters must be functions."
}
//get array going to be iterated
let arr = this;
if(!Array.isArray(arr)){
throw "You can only use forEach on arrays."
}
for(let index=0; index<arr.length; index++){ fn(arr[index],index,arr) } }Copy the code
filter
Array filter parsing:
- Using an array
- Filter items that do not return true from the callback function
Array.prototype.filter = function(fn){
if(typeoffn ! = ="function") {throw "Parameters must be functions."
}
//get array going to be iterated
let arr = this;
if(!Array.isArray(arr)){
throw "You can only use forEach on arrays."
}
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;
}
Copy the code
find
Array find parsing:
- Using an array
- Returns the first callback that returns true, or undefined
Array.prototype.find= function(fn){
if(typeoffn ! = ="function") {throw "Parameters must be functions."
}
//get array going to be iterated
let arr = this;
if(!Array.isArray(arr)){
throw "You can only use forEach on arrays."
}
for(let index=0; index<arr.length; index++){let invokedReturn = fn(arr[index],index,arr);
if( invokedReturn ){
return arr[index]
}
}
return void 0;
}
Copy the code
every
Array every parse:
- Using an array
- True if all callbacks return true, false otherwise
Array.prototype.every= function(fn){
if(typeoffn ! = ="function") {throw "Parameters must be functions."
}
//get array going to be iterated
let arr = this;
if(!Array.isArray(arr)){
throw "You can only use forEach on arrays."
}
for(let index=0; index<arr.length; index++){let invokedReturn = fn(arr[index],index,arr);
if( !invokedReturn ){
return false; }}return true;
}
Copy the code
some
Array some parsing:
- Using an array
- The result of a callback function is true if one returns true, and false otherwise
Array.prototype.some= function(fn){
if(typeoffn ! = ="function") {throw "Parameters must be functions."
}
//get array going to be iterated
let arr = this;
if(!Array.isArray(arr)){
throw "You can only use forEach on arrays."
}
for(let index=0; index<arr.length; index++){let invokedReturn = fn(arr[index],index,arr);
if( invokedReturn ){
return true; }}return false;
}
Copy the code
reduce
Array reduce parsing:
- Using an array
- One more argument than the other methods, the return value of the last call
- The last callback returns the result of reduce
- You can specify cumulative initial values and do not specify traversal from the second item
Array.prototype.reduce= function(fn,accumulator){
if(typeoffn ! = ="function") {throw "Parameters must be functions."
}
//get array going to be iterated
let arr = this;
if(!Array.isArray(arr)){
throw "You can only use forEach on arrays."
}
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 ;
}
Copy the code
The last
Thanks for reading!