一、map
1.作用:对每个元素加工再返回新集合
2.代码实现:
function myMap(fn, thisArg) {
if(typeof fn !== 'function') throw new Error('callback must be a function')
if(typeof thisArg == undefined) throw new Error('thisArg must not be null or undefined')
let originArr = this,
len = originArr.length;
let result = [];
for (var i = 0;i < len;i++) {
if(i in originArr){
let singleResult = fn.call(thisArg, originArr[i],i,originArr);
result.push(singleResult);
}
}
return result;
}
Array.prototype.myMap = myMap;
let arr = [1, 2, 3];
console.log(
arr.myMap(
function(item) {
return item * 2;
},
{ name: "name" }
)
);
3.失误:
(1)遍历数组时用for of
(2) 忘记给回调函数传入第三个参数-遍历数组本身
(3)没有检查callback和thisArg参数
二、forEach
1.用法
2.代码实现
function myForEach(fn,thisArg){
let originArr = this,
len = originArr.length;
for(var i = 0;i<len;i++){
if(i in originArr){
fn.call(thisArg,originArr[i],i,originArr)
}
}
}
Array.prototype.myForEach = myForEach
let arr = [1,2,3];
arr.myForEach(function(item,index){
console.log(item,index,'iterator')
},{name:'my thisArg'})
三、filter
1.用法
2.代码实现
function myFilter(callback, thisArg) {
if (typeof callback !== "function") {
throw new Error("callback must be a function");
}
if (typeof thisArg == null) {
throw new Error("thisArg must not be null or undifined");
}
let originArr = this,
len = this.length;
let result = [];
for (var i = 0; i < len; i++) {
if(i in originArr){
let passed = callback.call(thisArg, originArr[i], i, originArr);
if (passed) result.push(originArr[i]);
}
}
return result;
}
Array.prototype.myFilter = myFilter;
let arr = [1, 2, 3];
console.log(
arr.myFilter(
function(item) {
return item > 1;
},
{ name: "name" }
)
);
四、reduce
1.用法
遍历数组累计计算返回结果
参数:callback和initialValue
2.代码
function myReduce(callback,initialValue){
if(typeof callback !== 'function') throw new Error('callback must be a function')
let originArr = Object(this),
k = 0,
len = originArr.length;
let accumulotar = initialValue;
console.log(accumulotar)
if(accumulotar === undefined){
while(k < len && !(k in originArr)){
k++
}
if(k >= len){
throw new Error('Reduce of empty array with no initial value')
}
accumulotar = originArr[k++]
}
while(k < len){
if(k in originArr){
console.log(k)
accumulotar = callback.call(undefined,accumulotar,originArr[k],k,originArr)
}
k++
}
return accumulotar
}
Array.prototype.myReduce = myReduce
let arr = new Array(2);
arr.push(1)
arr.push(2)
console.log(arr.myReduce((accumulotar,element)=>{return accumulotar + element},8))
五、Function.prototype.apply
1.用法
改变函数执行上下文this指向,借用函数(Object.prototype.toString, 构造函数借用)
2.代码
function myApply(thisArg = window, ...args) {
let originFunction = this;
if (typeof originFunction !== "function") {
throw new Error("type error");
}
let functionName = Symbol();
thisArg[functionName] = originFunction;
return thisArg[functionName](args);
}
Function.prototype.myApply = myApply
六、模拟new关键字
1.用法
(1)先一个新对象并继承构造函数原型
(2)借用构造函数给新对象添加属性
(3)根据构造函数中的返回值,来判断返回新对象,还是构造函数的返回值
2.代码
function myNew(constructor,...args){
let instance = Object.create(constructor.prototye),
result = constructor.apply(instance,args),
resultType = typeof result;
let isObject = resultType === 'object' && !== null,
isFunction = resultType === 'function';
return isObject || isFunction ? result : instance
}