nodejs, basic question: What is this syntax?










-1














I am learning nodejs, coming from other languages (C#, etc) and some of the syntax has be confused.



For example this piece of code (I am sure it is fairly simple but would appreciate an explanation or at least a link to documentation that explains it)



 for(var index in files) 
console.log("-->"+index);
var task = (function(file)
return function()
fs.readFile(file, function(err, text)
if (err) throw err;
countWordsInText(text);
checkIfComplete();
);

)(filesDir + '/' + files[index]);
tasks.push(task);



what is this var task= (function(file)return function()......)(filesDir+.....);



there is a function that is calling a function and suddenly some parameters(??) outside?



I am guessing it is defining a list of functions but what is the rule for this syntax?










share|improve this question





















  • Possible duplicate of JavaScript closure inside loops – simple practical example
    – CertainPerformance
    Nov 12 at 10:31










  • It's an IIFE, but an unnecessarily confusing one - just use const instead of var and you avoid the need for it
    – CertainPerformance
    Nov 12 at 10:31











  • Immediately-invoked function expression, and albeit one that returns a function itself. Does nobody do computing science anymore?
    – Neil Lunn
    Nov 12 at 10:32










  • thanks! Apparently this is not CS but a "JavaScript programming language idiom" (wikipedia). Excellent link.
    – KansaiRobot
    Nov 12 at 10:35















-1














I am learning nodejs, coming from other languages (C#, etc) and some of the syntax has be confused.



For example this piece of code (I am sure it is fairly simple but would appreciate an explanation or at least a link to documentation that explains it)



 for(var index in files) 
console.log("-->"+index);
var task = (function(file)
return function()
fs.readFile(file, function(err, text)
if (err) throw err;
countWordsInText(text);
checkIfComplete();
);

)(filesDir + '/' + files[index]);
tasks.push(task);



what is this var task= (function(file)return function()......)(filesDir+.....);



there is a function that is calling a function and suddenly some parameters(??) outside?



I am guessing it is defining a list of functions but what is the rule for this syntax?










share|improve this question





















  • Possible duplicate of JavaScript closure inside loops – simple practical example
    – CertainPerformance
    Nov 12 at 10:31










  • It's an IIFE, but an unnecessarily confusing one - just use const instead of var and you avoid the need for it
    – CertainPerformance
    Nov 12 at 10:31











  • Immediately-invoked function expression, and albeit one that returns a function itself. Does nobody do computing science anymore?
    – Neil Lunn
    Nov 12 at 10:32










  • thanks! Apparently this is not CS but a "JavaScript programming language idiom" (wikipedia). Excellent link.
    – KansaiRobot
    Nov 12 at 10:35













-1












-1








-1







I am learning nodejs, coming from other languages (C#, etc) and some of the syntax has be confused.



For example this piece of code (I am sure it is fairly simple but would appreciate an explanation or at least a link to documentation that explains it)



 for(var index in files) 
console.log("-->"+index);
var task = (function(file)
return function()
fs.readFile(file, function(err, text)
if (err) throw err;
countWordsInText(text);
checkIfComplete();
);

)(filesDir + '/' + files[index]);
tasks.push(task);



what is this var task= (function(file)return function()......)(filesDir+.....);



there is a function that is calling a function and suddenly some parameters(??) outside?



I am guessing it is defining a list of functions but what is the rule for this syntax?










share|improve this question













I am learning nodejs, coming from other languages (C#, etc) and some of the syntax has be confused.



For example this piece of code (I am sure it is fairly simple but would appreciate an explanation or at least a link to documentation that explains it)



 for(var index in files) 
console.log("-->"+index);
var task = (function(file)
return function()
fs.readFile(file, function(err, text)
if (err) throw err;
countWordsInText(text);
checkIfComplete();
);

)(filesDir + '/' + files[index]);
tasks.push(task);



what is this var task= (function(file)return function()......)(filesDir+.....);



there is a function that is calling a function and suddenly some parameters(??) outside?



I am guessing it is defining a list of functions but what is the rule for this syntax?







node.js syntax






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 10:30









KansaiRobot

8661026




8661026











  • Possible duplicate of JavaScript closure inside loops – simple practical example
    – CertainPerformance
    Nov 12 at 10:31










  • It's an IIFE, but an unnecessarily confusing one - just use const instead of var and you avoid the need for it
    – CertainPerformance
    Nov 12 at 10:31











  • Immediately-invoked function expression, and albeit one that returns a function itself. Does nobody do computing science anymore?
    – Neil Lunn
    Nov 12 at 10:32










  • thanks! Apparently this is not CS but a "JavaScript programming language idiom" (wikipedia). Excellent link.
    – KansaiRobot
    Nov 12 at 10:35
















  • Possible duplicate of JavaScript closure inside loops – simple practical example
    – CertainPerformance
    Nov 12 at 10:31










  • It's an IIFE, but an unnecessarily confusing one - just use const instead of var and you avoid the need for it
    – CertainPerformance
    Nov 12 at 10:31











  • Immediately-invoked function expression, and albeit one that returns a function itself. Does nobody do computing science anymore?
    – Neil Lunn
    Nov 12 at 10:32










  • thanks! Apparently this is not CS but a "JavaScript programming language idiom" (wikipedia). Excellent link.
    – KansaiRobot
    Nov 12 at 10:35















Possible duplicate of JavaScript closure inside loops – simple practical example
– CertainPerformance
Nov 12 at 10:31




Possible duplicate of JavaScript closure inside loops – simple practical example
– CertainPerformance
Nov 12 at 10:31












It's an IIFE, but an unnecessarily confusing one - just use const instead of var and you avoid the need for it
– CertainPerformance
Nov 12 at 10:31





It's an IIFE, but an unnecessarily confusing one - just use const instead of var and you avoid the need for it
– CertainPerformance
Nov 12 at 10:31













Immediately-invoked function expression, and albeit one that returns a function itself. Does nobody do computing science anymore?
– Neil Lunn
Nov 12 at 10:32




Immediately-invoked function expression, and albeit one that returns a function itself. Does nobody do computing science anymore?
– Neil Lunn
Nov 12 at 10:32












thanks! Apparently this is not CS but a "JavaScript programming language idiom" (wikipedia). Excellent link.
– KansaiRobot
Nov 12 at 10:35




thanks! Apparently this is not CS but a "JavaScript programming language idiom" (wikipedia). Excellent link.
– KansaiRobot
Nov 12 at 10:35












2 Answers
2






active

oldest

votes


















1














It's called IIFE (Immediately Invoked Function Expression). Basically, you define a function



function ()


and immediately execute it



(function())();


What the code you've posted is doing is, executing a function and storing the returned value in task.



Hope this helps.






share|improve this answer




















  • Thanks! Very helpful!
    – KansaiRobot
    Nov 12 at 10:37


















0














Thats an IIFE (Immediately Invoked Function Expression). Basically it's a JavaScript function that runs as soon as it is defined.



(function () 
statements
)();


Taken right from mozi//a: -
It is a design pattern which is also known as a Self-Executing Anonymous Function and contains two major parts. The first is the anonymous function with lexical scope enclosed within the Grouping Operator (). This prevents accessing variables within the IIFE idiom as well as polluting the global scope.



The second part creates the immediately executing function expression () through which the JavaScript engine will directly interpret the function.






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53260256%2fnodejs-basic-question-what-is-this-syntax%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    It's called IIFE (Immediately Invoked Function Expression). Basically, you define a function



    function ()


    and immediately execute it



    (function())();


    What the code you've posted is doing is, executing a function and storing the returned value in task.



    Hope this helps.






    share|improve this answer




















    • Thanks! Very helpful!
      – KansaiRobot
      Nov 12 at 10:37















    1














    It's called IIFE (Immediately Invoked Function Expression). Basically, you define a function



    function ()


    and immediately execute it



    (function())();


    What the code you've posted is doing is, executing a function and storing the returned value in task.



    Hope this helps.






    share|improve this answer




















    • Thanks! Very helpful!
      – KansaiRobot
      Nov 12 at 10:37













    1












    1








    1






    It's called IIFE (Immediately Invoked Function Expression). Basically, you define a function



    function ()


    and immediately execute it



    (function())();


    What the code you've posted is doing is, executing a function and storing the returned value in task.



    Hope this helps.






    share|improve this answer












    It's called IIFE (Immediately Invoked Function Expression). Basically, you define a function



    function ()


    and immediately execute it



    (function())();


    What the code you've posted is doing is, executing a function and storing the returned value in task.



    Hope this helps.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 12 at 10:36









    Manish Gharat

    31618




    31618











    • Thanks! Very helpful!
      – KansaiRobot
      Nov 12 at 10:37
















    • Thanks! Very helpful!
      – KansaiRobot
      Nov 12 at 10:37















    Thanks! Very helpful!
    – KansaiRobot
    Nov 12 at 10:37




    Thanks! Very helpful!
    – KansaiRobot
    Nov 12 at 10:37













    0














    Thats an IIFE (Immediately Invoked Function Expression). Basically it's a JavaScript function that runs as soon as it is defined.



    (function () 
    statements
    )();


    Taken right from mozi//a: -
    It is a design pattern which is also known as a Self-Executing Anonymous Function and contains two major parts. The first is the anonymous function with lexical scope enclosed within the Grouping Operator (). This prevents accessing variables within the IIFE idiom as well as polluting the global scope.



    The second part creates the immediately executing function expression () through which the JavaScript engine will directly interpret the function.






    share|improve this answer

























      0














      Thats an IIFE (Immediately Invoked Function Expression). Basically it's a JavaScript function that runs as soon as it is defined.



      (function () 
      statements
      )();


      Taken right from mozi//a: -
      It is a design pattern which is also known as a Self-Executing Anonymous Function and contains two major parts. The first is the anonymous function with lexical scope enclosed within the Grouping Operator (). This prevents accessing variables within the IIFE idiom as well as polluting the global scope.



      The second part creates the immediately executing function expression () through which the JavaScript engine will directly interpret the function.






      share|improve this answer























        0












        0








        0






        Thats an IIFE (Immediately Invoked Function Expression). Basically it's a JavaScript function that runs as soon as it is defined.



        (function () 
        statements
        )();


        Taken right from mozi//a: -
        It is a design pattern which is also known as a Self-Executing Anonymous Function and contains two major parts. The first is the anonymous function with lexical scope enclosed within the Grouping Operator (). This prevents accessing variables within the IIFE idiom as well as polluting the global scope.



        The second part creates the immediately executing function expression () through which the JavaScript engine will directly interpret the function.






        share|improve this answer












        Thats an IIFE (Immediately Invoked Function Expression). Basically it's a JavaScript function that runs as soon as it is defined.



        (function () 
        statements
        )();


        Taken right from mozi//a: -
        It is a design pattern which is also known as a Self-Executing Anonymous Function and contains two major parts. The first is the anonymous function with lexical scope enclosed within the Grouping Operator (). This prevents accessing variables within the IIFE idiom as well as polluting the global scope.



        The second part creates the immediately executing function expression () through which the JavaScript engine will directly interpret the function.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 at 10:46









        Nelson Owalo

        1,062925




        1,062925



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53260256%2fnodejs-basic-question-what-is-this-syntax%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            ReactJS Fetched API data displays live - need Data displayed static

            政党