Javascript callback function Understanding---Two WA buy kidney machine 6

Source: Internet
Author: User

In JavaScript, what is a callback function, I think simply is to pass a function B as a parameter to another function A, in a function in a certain time call function B.

As you can see here, the callback function forms a closure that accesses the active object in function A.

Please see the following code:

A village two WA like buy kidney machine 6, but the Apple Store found wood inventory, ready to go to stock, then this time two WA have a choice:

One is:

After a while to ask: "Arrived?" "," No "!

After a while to ask: "Arrived?" "," No "!

After a while to ask: "Arrived?" "," No "!

After a while to ask: "Arrived?" "," No "!

We call this the polling method, which is relatively inefficient, and the two WA legs have run off.

At this time there is a way, you can leave a phone number, let the fruit shop after the arrival of the second WA to buy, so that two WA fart to do other work, wait for notice.

Here, to the fruit Shop Query method, left the mobile phone number, this mobile phone number is a callback function. The fruit store passes the arrival quantity to the callback function, which is the callback function with the return parameter.

varSeller={stock:"Iphone6", Stockqty:0, query:function(Something, quantity, phone13988888888) {Console.log ("Do you want to buy" + Something + "? "); if( This. stock==something) {Console.log ("Yes, we sell" + ", but let me check the inventory first.); if( This. Stockqty <quantity) {Console.log ("Sorry we sold out, you see the library---" + Something + "inventory number is:" + This. Stockqty + "---, please leave a mobile phone number, after arrival notify you to take! ") Sleep (0.01);  This. Stockqty = 100; Console.log ("We are arriving, the quantity I will call you in a minute!" "); Console.log ("Dial Now:") phone13988888888 ( This. Stock, This. Stockqty); }Else{Console.log ("Stock enough, immediate delivery---" +something+ ":" +quantity+ "---Taiwan! ");  This. stockqty= This. stockqty-quantity; returnquantity; }        }Else{Console.log ("Sorry, we don't sell:" +something+ "Please look elsewhere!" "); }    }};functionSleep (delay) {varStart =NewDate (). GetTime ();  while(NewDate (). GetTime () < start +delay) {Console.log ("In stock ..."); };}functionphone13988888888 (item,quantity) {Console.log ("Received the phone, the Apple store said they came---" +item+ ":" +quantity+ "---Taiwan!"); if(quantity>0) {Console.log ("I can buy it this time, aha haha!"); varqty=2; Console.log ("and ran to the fruit shop, shouting boss, kidney machine 6来:--" +qty+ "--Taiwan! "); varReceiver=seller.query ("Iphone6", qty); if(receiver===qty) {Console.log ("Received" +item+ ":" +qty+ "stage, the transaction is complete! ")}}}console.log ("The owner of the iphone6 is very friendly." "); Seller.query ("Iphone6", 1, phone13988888888); Seller.query ("Iphone7", 1,phone13988888888);

Then look at the output:

Two WA: Boss, kidney machine iphone6 to one! Shop Small Two: Do you want to buy iphone6? Shop Small Two: yes we sell, but let me first check inventory store small two: sorry we sold out, you look at the library ---iphone6 inventory number is: 0---, please leave a mobile phone number, after the arrival of the notice you come to fetch! Shop Small two: in stock ... Shop Small two: in stock ... Shop Small two: in stock ... Shop Small two: in stock ... Shop Small two: in stock ... Shop Small Two: We arrived, the quantity of goods I will call you in a minute! Shop Small Two: dial now: Two WA: Received the phone, the Apple store said they came ---iphone6:100---Taiwan! Two WA: This time can buy, aha haha ! two WA: Run to the fruit shop, Yell boss, kidney machine 6来:--2-- Taiwan! Shop Small Two: Do you want to buy iphone6? Shop Small Two: yes we sell, but let me first check the inventory store small two: inventory enough, immediate delivery ---iphone6:2--- Taiwan! Two WA: Received iphone6:2 Taiwan, the transaction is complete! Shop Small Two: Do you want to buy Iphone7? Shop Small Two: Sorry, we do not sell: Iphone7 please look elsewhere! 

Javascript callback function Understanding---Two WA buy kidney machine 6

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.