Why code javascript with functions having same ending parameters as input parameters?

This question already has an answer here:

I come from "old school" javascript functions which I don't even need to demonstrate.

  1. What is the advantages of the following style?
  2. Is this self executing?
  3. How is this even called?
  4. What is the point of ($, reportGroupDataManager, data) ?
  5. What is this coding style called?
  6. Where can I learn how to code this style , where and how?

thx in advance

    (function(jQ, dM, data) {
       var self = this;
       //var $container = jQ('#menu-tree'),
       //    initializePage = function(resources) {
       //    console.log('in init');

       //        //var resources = "blah";
       //    };

       var initializePage = function () {
           console.log('in init');



    })($, reportGroupDataManager, data);

Answers 1

    1. Advantage of this style is all your logic is wrapped by this self invoking function. So the variables used are not in the global scope. Good for garbage collection. As all variables are local to the scope so your code is safe.
    2. Yes it is self executing.
    3. It is called because of the parenthesis in the end. ().
    4. You are also passing values using ($, reportGroupDataManager, data) which can be used inside the function.
    5. This is called self invoking function. The inside function is called anonymous function. As ou are wrapping the function like this ( function goes here...)(); So it is self invoking as it is getting called by doing so.
    6. You can search for self invoking function in javascript and get lots of resources.

    Hope it helps.

Related Articles