Home>

Some parts of the following code are unknown.

<! DOCTYPE html>
<html lang = "en">
<head>
    <meta charset = "utf-8">
</head>
<body>
    <script>
    function closure (init) {
        var counter = init;
        return function () {
            return ++ counter;
        }
    }
    var myClosure = closure (1);
    console.log (myClosure ());// Result: 2
    console.log (myClosure ());// Result: 3
    console.log (myClosure ());// Result: 4
    </script>
</body>
</html>


In the above code, in the part where "var myClosure = closure (1);", I think that the variable counter has already become 2 in the following part, but the variable in the following part, counter Is incremented, why isn't the part executed, and in the first console.log (myClosure ());

return function () {
            return ++ counter;
        }


If anyone can explain, thank you.

  • Answer # 1

    The function is not executed just by creating a function

    function closure (init) {
            var counter = init;
            console.log ('create closure');
            return function () {
                console.log ('execute closure');
                return ++ counter;
            }
        }
        var myClosure = closure (1);
        console.log ('created closure');
        console.log (myClosure ());
        console.log (myClosure ());
        console.log (myClosure ());
    / *
    result
    [Log] create closure
    [Log] created closure
    [Log] execute closure
    [Log] 2
    [Log] execute closure
    [Log] 3
    [Log] execute closure
    [Log] 4
    */

  • Answer # 2

    From the previous answer, it seems like a snake foot, but it is complementary.

    The functionclosureis "a function that returns a" function that returns a value "". So, it is a "function" that can be obtained by writingclosure (something)to the right of=.

    function add (a, b) {// Function that returns a value
      return a + b;
    }
    const c = add (1, 2);// c is the value
    function adder (a) {// Function that returns a "function that returns a value"
      return function (b) {return a + b;}
    }
    const d = adder (1);// d is a function
    const e = d (2);// e = 3;

    var myClosure = closure (1);is 1, but in detailclosure"passes the value received by itself" Since it is supposed to be a function that returns a value calculated from the stored value, it is not a strong mistake to understand "initial value".

    As a result of executing

    return, an executable function object is created and returned from the function declaration as described in the source code. However, the body offunction(the contents of{}) is not executed until a"function call"occurs, so the name associated with the function isIf the function is not called like the name ()given to the function, it will not be calculated (so++occurs first inconsole.log ( myClosure ());// result: 2location)

  • Answer # 3

      

    I think that the variable counter is already 2 in the following part in the part that says "var myClosure = closure (1);"

    closure (1)timing does not cause++ counterincrementing because the comprehension function isnot executed.
    To call a function expression, you must add()as shown below.

    (function () {
      console.log ('execute closure');
      return ++ counter;
    } ());

    Re: newyee