Js deep learning-code reuse of callback Functions
In js, a code block is often used repeatedly in multiple places. This method is not conducive to code optimization, and it is also troublesome for personnel maintenance in the future, if the reuse code block needs to be modified in the later stage, only one of them will occur, resulting in problems. In fact, the process is very simple.
Extract the reusable code block and write it as a function. Simply call this function elsewhere.
For example, two functions F1 and F2 both execute code block CODE1. We can encapsulate CODE1 into F3,
Funcitn F1 () {F3} funcitn F2 () {F3}
However, in actual situations, F3, F4 must be executed in F1, and F3 and F4 must be submitted asynchronously in Ajax. However, F4 must execute AJAX requests in F3 and return TRUE,. Suppose the code is as follows:
Funcitn F1 () {F3f4}
Certainly not, because F3 and F4 are both asynchronous requests, F3 has not been executed yet, maybe F4 has started to be executed, so how can F4 be executed after F3 execution is complete.
However, if F3 is restored to a code block, it can be solved, but it cannot achieve the effect of code reuse. You only need to add a CallBack parameter to F3.
The Code is as follows.
Function F3 (CallBack) {$. post (). done (funciton (result) {if (result = true & callBack) callBack () ;};} funciton F2 () {F3 (F4 )}}