Higher-order functions

To understand AOP, you must first understand higher-order functions, which require two things:

  • The argument to a function is a function (callback, passing a function to another function).
  • Return function () {}

AOP

Slice-oriented programming, that is, the code is twice packaged, on the basis of not destroying the original logic, insert their own logic, the following example, add new logic to the original say function:

// Now there is a say function, which prints' say~~ 'function say() {
  console.log('say~~'); } // What if we want to add our own logic to the say function? What we want is something like thislet newSay = say.before(function() {// Execute console.log() before the original logic executes.'hello')}); newSay(); // We can usefunctionThe prototype chain of the Function. The prototype. Before =function(fn) {
  let that = this;
  return function() { fn(); that.apply(that,arguments); }} /** Result: Hello say~~ uncle aunt */Copy the code