-1

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?

    (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');
    
    
       };
    
    
    
       dM.getResources()
          .done(initializePage);
    
    
    })($, reportGroupDataManager, data);
    
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • $ = jQ so that is referring to jquery, you or your project or someone's project probably had a reportGroupDataManager.js file -- that would help you a bit , there is not much code provided to help you understand IMO, probably some "javascript ninjas" that know all the patterns and styles that hopefully can address your questions better – Tom Stickel Apr 06 '16 at 04:56

2 Answers2

0

This code snippet defines a anonymous function and then invoke it with given parameters.

it works as same as the following code:

function funt(a, b) {return a + b}
funt(1, 2); // or window.funt(1, 2);

The advantage of the simple code style is you wont create variables in global context:

(function(a, b){return a + b;})(1, 2);

Saying global context, there is window object in web page and global in nodejs.

And the other advantage is that you can do anything u want inside the function, like define event handlers, and the local variables are hidden by this function.

hoffman
  • 42
  • 4
  • ok, ya i understand what you are saying , what can i read and watch online to learn this more? –  Apr 06 '16 at 05:02
  • Recommendations for webpages, tutorials etc are off-topic at SO. Look up the term in Google. – Andy Apr 06 '16 at 05:05
  • @hoffman question http://stackoverflow.com/questions/36444046/with-a-javascript-iffe-getting-a-uncaught-typeerror-is-not-a-function –  Apr 06 '16 at 07:21
0
  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.

rajesh
  • 2,354
  • 1
  • 12
  • 15
  • "passing values using ..... " so am I passing the values ? Any jsfiddle or plunker examples? –  Apr 06 '16 at 05:07
  • Yes.. you are passing $, reportGroupDataManager, data which are captured by jQ, dM, data respectively. Check this jsfiddle. https://jsfiddle.net/mishrarajesh/g02pgmq2/ – rajesh Apr 06 '16 at 05:22
  • thank you for that –  Apr 06 '16 at 07:05
  • ok question here http://stackoverflow.com/questions/36444046/with-a-javascript-iffe-getting-a-uncaught-typeerror-is-not-a-function –  Apr 06 '16 at 07:21