javascript - Mind boggling Function for Looping -


If there is someone who can help me understand this one, then I will be impressed. It's from # 28.

Here is a function for looping, and, if you look at the tutorial, it seems that it produces 6 pass below 3 times.

Please explain to you what is happening in this program, as explained in plain language in as much detail as possible:

  1. fn call (array, array [i], i) function, and how do they all work, before the function array, then the array [i], And then I? What is happening when everything is happening?

  2. In addition, in the function, I understand that i ++ goes up every time that it goes through the array length, but increasing its value What does num ++ trigger for, and how to value == num ++

  3. in < Code> Function (value, i) , what is value ? Is the value optional 0,1,2 of the loop array? If so, then those loop array numbers are used as a parameter in the function (value, i)

  4. ? Code> What is the attempt to show it? how?.

    Code:

      function loop (array, FN) {for (var i = 0; i & lt; array length; i ++) fn.call (array, array [i], i); } Var num = 0; Loop ([0, 1, 2], function (value, i) {emphasis (value == num ++, "make sure the contents are as good as possible"); (This presentation means " The reference should be an absolute array. ");});   
    PASS Ensure that we expect this as the content.
    The PASS reference should be a whole array.
    Ensure that as soon as material is expected. The pass reference should be a whole array.
    Ensure that as soon as material is expected. The pass reference should be a whole array.

    function is an anonymous function that works without a name < The complete second argument of the code> loop

      is the function (value, i) {Enter the value (value == num ++, "make sure the contents are expected as we Expect. "); Emphasis (this example is, "References should be a whole array."); }   

    which is a complete anonymous task that takes three arguments: this (for which the object is a method), the current value, and the loop counter .

    loop runs over the array again, and uses fn.call to call an anonymous function, it gives three arguments Done; Here, the array object should be clear, because call does not know which reference should be applied to the context in which context it is being applied (which is what this To call) in the call).

    Anonymized function, as is brought by loop , is received in the form of this . The second ASSERT verifies this also expects that the value of the array is [0, 1, 2] and on each call the num Enlarges and verifies it by comparing it with aligned array element.

    Therefore, after the execution series:

    1. num is declared and is initialized on 0.
    2. The loop ([0, 1, 2], the function ...) is applied.
    3. loop invokes fn , with anonymous function, array (as this ), its first element, and i which indicates the element offset ( i is actually never used.)
    4. Unknown function ASSERT Comparing it with and num later on.
    5. The unknown function is ASSERT that its it a array .
    6. loop invokes fn as in # 3, but from the second array element.
    7. Anonymize function again sends its ASSERT , this time it compares the second array element aligned, which would be 1 against num Hopefully (which is post-step 4 increments, 1).
    8. loop invokes fn before the third array element.
    9. Anonymize function redoes its ASSERT s, against the current num , the expected array compares the element 2, whose value is 2.

Comments