Is this an immediately invoked function expression?

前端 未结 3 1073
广开言路
广开言路 2021-01-28 02:14

I\'m a javascript newbie trying to wrap my mind around this code. I got it here http://brackets.clementng.me/post/24150213014/example-of-a-javascript-closure-settimeout-inside

3条回答
  •  我在风中等你
    2021-01-28 03:10

    The parantheses are optional in your example of an immediately invoked function.

    (function() {
      // some code
    })();
    

    Can be re-written as

    function() {
      // some code
    }();
    

    So the i in the example function's call becomes the x in the function definition.

    function(x) { // x = 1
        return function() { 
            console.log(x); // x = 1
        }; 
    }(1)
    

提交回复
热议问题