The After function of the currization of the function is applied

Usage: this function will not be executed until the number of calls you specify

Currie, what is the function on wikipedia says currie, English: Currying (both in translation is indeed full of visual sense), is a function of the accept multiple parameters to accept a single parameter (the original function of the first parameter) function, and return to accept the rest of the parameters and return the result of the new function of technology. Here do not do too much introduction, should know all know, do not know slowly understand


// First define an after function that takes two arguments
// count is used to subtract to determine when your function is executed
// callback is the callback to execute
function after (count, callback) {
  let total = 0 // Declare a counter to augment the function (you can remove it if you don't need it)
  return function (value) { // value Accepts the value 1 in fn(1)
    total += value
    if (--count == 0) {
      // If the function is subtracted to 0, the callback is executed, that is, the contents of fn below are executed, passing total as well
      callback(total)
    }
  }
}

let fn = function (params) {
  console.log(Q: how handsome am I in the world? '.'ANSWER: first' + params + '! ')}let handsomeBoy = after(3, fn)

handsomeBoy(1)
handsomeBoy(1)
handsomeBoy(1) // Q: I am the world's most handsome? Answer: number 3!
Copy the code

The first two handsomeBoy(1) calls will not be executed until the third time because if fails, making me the third handsome in the world.

The implementation of the above after function, if continued to the handsomeBoy(1) function to the fourth, fifth… How do you shake my ranking as the world’s third richest man?

if (--count <= 0) { // Modify a small symbol here
    callback(total)
}

handsomeBoy(1)
handsomeBoy(1)
handsomeBoy(1) // Q: I am the world's most handsome? Answer: number 3!
handsomeBoy(1) // Q: I am the world's most handsome? A: Number four!
handsomeBoy(6) // Q: I am the world's most handsome? A: Number 10!

Copy the code

Change the if statement (–count == 0) to (–count <= 0), after the limit of 3 times, each call will be executed.

AOP’s before function for slice programming is applied

The main solution is to modify other people’s functions, in order not to directly insert their own code, resulting in high coupling degree, and in line with the principle of not invading other people’s code and improve readability

Function:

function say (a, b) {
  console.log(a + 'Eyes open'+ b) ... Business logic}Copy the code

It’s written in an intrusive way

function say (a, b) {
  before()
  console.log(a + 'Eyes open'+ b) ... Business logic after()}function before(){
  console.log('Before - Invasion invasion invasion! ')}function after(){
  console.log('After - invasion invasion invasion! ')
}

say('Give me that.'.'! ')
Copy the code

Output:

Before – Invasion invasion invasion! What an eye-opener! After – Invasion invasion invasion! While this is convenient and quick for console developers, it has a number of disadvantages:

  • It’s inefficient to write every function once
  • The original code is changed to accommodate new requirements, resulting in higher coupling
  • Low readability, let later people can not understand the real purpose of the code, a good function, why before, after

AOP writing

A prototype chain is used

function speak (a, b) {
  console.log(a + 'Eyes open'+ b) ... Business logic}// Add a before method to the prototype chain
Function.prototype.before = function (fn) {
  // This refers to whoever calls this, where this refers to the speak function
  let that = this
  return function () {
    // This refers to the window
    // fn is the function content below.before
    fn()
    // that is speak
    // Expand arguments to get all argumentsthat(... arguments) } }let beforeSpeak = speak.before(function () {
  console.log(Knife in the ass)
})

beforeSpeak('Give me that.'.'! ')
Copy the code

Output: Knife prick buttocks really open my eyes! By slicing, we not only complete the requirements but also ensure the integrity of the original function, and can indirectly pass a message to the later comers. The content added in. Before is added in the requirements of the later update iteration, not the original one.