Higher-order functions

A higher-order function is a function that satisfies at least one of the following conditions.

  • Functions can be passed as arguments;
  • The function can be output as a return value.

For example, functions can be passed as arguments.

function aFn(callback) {
    callBack&&callBack();
}

function bFn(n) {
    return n+1;
}

aFn(bFn)

Copy the code

In this case, aFn is a higher-order function because it can take a function as an argument.

On the other hand, the function can be output as a return value, which is even simpler, as was the case with closures in the previous section.

Every function that produces a closure is a higher-order function.

Aspect Oriented Programming (AOP)

Implementing AOP with decorator functions:

var check(fn) {
    return (a,b) = > {
        // Cut the surface
        if (a<b) {
            throw new Error('Unmanageable')}return fn(a,b);
    }
}

check((a,b) = >{
    return 1
})


check((a,b) = >{
    return 2
})
Copy the code

Use js decorator implement AOP: blog.yodfz.com/frontEnd/20…

For more advanced syntax, see Proxy and Reflect.

Function currying

Currying is also called partial evaluation. A function that curries will first take some arguments. After taking these arguments, the function will not immediately evaluate, but will continue to return another function, and the arguments that were passed will be stored in the closure formed by the function. When the function is actually asked for a value, all the arguments passed in before are used to evaluate it at once.

Function of the throttle

The purpose of function throttling is to save device resources and control the trigger amount. The idea is to trigger only once at a fixed time. If it has been triggered once within a fixed period of time, all remaining requests are abandoned.

Time-sharing function

Delay the execution of the current function and ignore subsequent requests for that function if the delay has not been completed.

Lazy loading function

Lazy loading functions occur because subsequent methods are dependent on the client environment or preloaded requirements, resulting in a failure to provide uniform code.

It is up to lazy loading functions to handle these pre-requirements.

In some environmental judgment to do JS library gasket can be applied.

Link: blog.yodfz.com/admin/write…