1、Array.filter 数组过滤方法
Array.prototype.filter = function(callback,thisArg){ if(!this){ throw new TypeError('this is null or undefined') } if(typeof callback !=='function'){ throw new TypeError(callback+"is not a function") } let res = [] let obj = Object(this) // obj做为回调函数的参数 let len = obj.length>>>0 // 保证len是number,且是正整数 for(let i=0;i<len;i++){ if(i in obj){ // 检查i时候在obj的属性里面 ,会检查原型链、、 if(callback.call(thisArg,obj[i],i,obj)){ res.push(obj[i]) } } } return res}
2、Array.map 返回一个新数组
Array.prototype.map = function(callback,thisArg){ if(!this){ throw new TypeError('this is null or undefined') } if(typeof callback !=='function'){ throw new TypeError(callback+"is not a function") } let res = [] let obj = Object(this) // obj做为回调函数的参数 let len = obj.length>>>0 // 保证len是number,且是正整数 for(let i=0;i<len;i++){ if(i in obj){ // 检查i时候在obj的属性里面 ,会检查原型链、、 res[i] = callback.call(thisArg,obj[i],i,obj) } } return res}
3、Array.forEach 循环数组,不返回值
Array.prototype.forEach = function(callback,thisArg){ if(!this){ throw new TypeError('this is null or undefined') } if(typeof callback !=='function'){ throw new TypeError(callback+"is not a function") } let obj = Object(this) // obj做为回调函数的参数 let len = obj.length>>>0 // 保证len是number,且是正整数 for(let i=0;i<len;i++){ if(i in obj){ // 检查i时候在obj的属性里面 ,会检查原型链、、 callback.call(thisArg,obj[i],i,obj) } }}
4、Array.reduce
Array.prototype.reduce = function(callback,initialValue){ if(!this){ throw new TypeError('this is null or undefined') } if(typeof callback !=='function'){ throw new TypeError(callback+"is not a function") } let start = 0 // let obj = Object(this) // obj做为回调函数的参数 let len = obj.length>>>0 // 保证len是number,且是正整数 let result = initialValue if(result===undefined){ while (start < len && !(start in obj)) { start++; } if(start>=len){ throw new TypeError('array is empty,with no initial value') } result = obj[start++] } for(let i=start;i<len;i++){ if(i in obj){ // 检查i时候在obj的属性里面 ,会检查原型链、、 result = callback.call(undefined,result,obj[i],i,obj) } } return result}
5、Function.prototype.apply
Function.prototype.apply=function(context,args){ context = context || window const fn = Symbol('fn') // 定义一个唯一标识 context[fn] = this // this 表示当前function,暂存下来后面运行 const res = context[fn](...args) delete context[fn] // 函数运行之后删除缓存 return res}const obj1={ name:'obj1'}const obj2={ name:'obj2'}const myName = function(name,key){ console.log(this.name||name)}myName.apply(obj1,['li','wang']) // 打印 obj1myName.apply(0,['li2','wang2']) // 打印 li2
6、Function.prototype.call
Function.prototype.call=function(context,...args){ context = context || window const fn = Symbol('fn') // 定义一个唯一标识 context[fn] = this // this 表示当前function,暂存下来后面运行 const res = context[fn](...args) delete context[fn] // 函数运行之后删除缓存 return res}const obj1={ name:'obj1',}const obj2={ name:'obj2'}const myName = function(name,key){ console.log(this.name||name)}myName.call(obj1,'li','wang') // 打印 obj1myName.call(0,'li2','wang2') // 打印 li2
7、Function.prototype.bind
Function.prototype.bind=function(context,...args){ let self = this return function F(...params){ if(this instanceof F){ return new self(...args,...params) } return self.apply(context,[...args,...params]) }}
8、debounce (防抖)
const debounce = (fn,time=300)=>{ let timer = null return function(...params){ clearTimeout(timer) timer = setTimeout(()=>{ fn.apply(this,[...params]) },time) }}
9、throttle(节流)
const throttle = (fn,time=300)=>{ let timer = null return function(...params){ if(!timer){ timer = setTimeout(()=>{ clearTimeout(timer) fn.apply(this,[...params]) },time) } }}
10、柯里化