Js deep learning-code reuse of callback Functions

Source: Internet
Author: User

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 )}}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.