高阶函数

问:什么是高阶函数?
答:1、一个函数的参数是函数,就是高阶函数(回调函数是高阶函数)
2、一个函数返回一个函数,当前的这个函数就是高阶函数。

 // 写了一个业务代码,扩展当前的业务代码
function say(a,b){
  console.log(a,b,'----say');
}

// 给某个方法添加一个方法,在他之前执行
Function.prototype.before = function (callback){
  return (...args)=>{ // 剩余运算符,箭头函数没有this,没有arguments。
    callback();
    this(...args);// 展开运算符  当前的this是say方法
  }
}
let sayBefore = say.before(()=>{
  console.log('-----beforeSay')
});
sayBefore('hello','world');
// 运行结果
------beforeSay
hello world -----say

你可能感兴趣的:(高阶函数)