1、Array.filter 数组过滤方法

  1. Array.prototype.filter = function(callback,thisArg){
  2. if(!this){
  3. throw new TypeError('this is null or undefined')
  4. }
  5. if(typeof callback !=='function'){
  6. throw new TypeError(callback+"is not a function")
  7. }
  8. let res = []
  9. let obj = Object(this) // obj做为回调函数的参数
  10. let len = obj.length>>>0 // 保证len是number,且是正整数
  11. for(let i=0;i<len;i++){
  12. if(i in obj){ // 检查i时候在obj的属性里面 ,会检查原型链、、
  13. if(callback.call(thisArg,obj[i],i,obj)){
  14. res.push(obj[i])
  15. }
  16. }
  17. }
  18. return res
  19. }

2、Array.map 返回一个新数组

  1. Array.prototype.map = function(callback,thisArg){
  2. if(!this){
  3. throw new TypeError('this is null or undefined')
  4. }
  5. if(typeof callback !=='function'){
  6. throw new TypeError(callback+"is not a function")
  7. }
  8. let res = []
  9. let obj = Object(this) // obj做为回调函数的参数
  10. let len = obj.length>>>0 // 保证len是number,且是正整数
  11. for(let i=0;i<len;i++){
  12. if(i in obj){ // 检查i时候在obj的属性里面 ,会检查原型链、、
  13. res[i] = callback.call(thisArg,obj[i],i,obj)
  14. }
  15. }
  16. return res
  17. }

3、Array.forEach 循环数组,不返回值

  1. Array.prototype.forEach = function(callback,thisArg){
  2. if(!this){
  3. throw new TypeError('this is null or undefined')
  4. }
  5. if(typeof callback !=='function'){
  6. throw new TypeError(callback+"is not a function")
  7. }
  8. let obj = Object(this) // obj做为回调函数的参数
  9. let len = obj.length>>>0 // 保证len是number,且是正整数
  10. for(let i=0;i<len;i++){
  11. if(i in obj){ // 检查i时候在obj的属性里面 ,会检查原型链、、
  12. callback.call(thisArg,obj[i],i,obj)
  13. }
  14. }
  15. }

4、Array.reduce

  1. Array.prototype.reduce = function(callback,initialValue){
  2. if(!this){
  3. throw new TypeError('this is null or undefined')
  4. }
  5. if(typeof callback !=='function'){
  6. throw new TypeError(callback+"is not a function")
  7. }
  8. let start = 0 //
  9. let obj = Object(this) // obj做为回调函数的参数
  10. let len = obj.length>>>0 // 保证len是number,且是正整数
  11. let result = initialValue
  12. if(result===undefined){
  13. while (start < len && !(start in obj)) {
  14. start++;
  15. }
  16. if(start>=len){
  17. throw new TypeError('array is empty,with no initial value')
  18. }
  19. result = obj[start++]
  20. }
  21. for(let i=start;i<len;i++){
  22. if(i in obj){ // 检查i时候在obj的属性里面 ,会检查原型链、、
  23. result = callback.call(undefined,result,obj[i],i,obj)
  24. }
  25. }
  26. return result
  27. }

5、Function.prototype.apply

  1. Function.prototype.apply=function(context,args){
  2. context = context || window
  3. const fn = Symbol('fn') // 定义一个唯一标识
  4. context[fn] = this // this 表示当前function,暂存下来后面运行
  5. const res = context[fn](...args)
  6. delete context[fn] // 函数运行之后删除缓存
  7. return res
  8. }
  9. const obj1={
  10. name:'obj1'
  11. }
  12. const obj2={
  13. name:'obj2'
  14. }
  15. const myName = function(name,key){
  16. console.log(this.name||name)
  17. }
  18. myName.apply(obj1,['li','wang']) // 打印 obj1
  19. myName.apply(0,['li2','wang2']) // 打印 li2

6、Function.prototype.call

  1. Function.prototype.call=function(context,...args){
  2. context = context || window
  3. const fn = Symbol('fn') // 定义一个唯一标识
  4. context[fn] = this // this 表示当前function,暂存下来后面运行
  5. const res = context[fn](...args)
  6. delete context[fn] // 函数运行之后删除缓存
  7. return res
  8. }
  9. const obj1={
  10. name:'obj1',
  11. }
  12. const obj2={
  13. name:'obj2'
  14. }
  15. const myName = function(name,key){
  16. console.log(this.name||name)
  17. }
  18. myName.call(obj1,'li','wang') // 打印 obj1
  19. myName.call(0,'li2','wang2') // 打印 li2

7、Function.prototype.bind

  1. Function.prototype.bind=function(context,...args){
  2. let self = this
  3. return function F(...params){
  4. if(this instanceof F){
  5. return new self(...args,...params)
  6. }
  7. return self.apply(context,[...args,...params])
  8. }
  9. }

8、debounce (防抖)

  1. const debounce = (fn,time=300)=>{
  2. let timer = null
  3. return function(...params){
  4. clearTimeout(timer)
  5. timer = setTimeout(()=>{
  6. fn.apply(this,[...params])
  7. },time)
  8. }
  9. }

9、throttle(节流)

  1. const throttle = (fn,time=300)=>{
  2. let timer = null
  3. return function(...params){
  4. if(!timer){
  5. timer = setTimeout(()=>{
  6. clearTimeout(timer)
  7. fn.apply(this,[...params])
  8. },time)
  9. }
  10. }
  11. }

10、柯里化