Search code examples
javascriptfunctionlogicclosuresiife

Reference a function inside an IIFE


I have the following logic:

var first_function = (function(d3, second_function) {
  function third_function(param1, param2) {
    /* do stuff here */
  }
})(d3, second_function);

Outside of an IIFE structure, to access the third function, I could normally do something like:

first_function.third_function(data1, data2);

Where am I going wrong?


Solution

  • Its not returning it, so the function is just evaporating. You can do something like this:

    var first_function = (function(d3, second_function) {
      function third_function(param1, param2) {
        /* do stuff here */
      }
      return third_function;
    })(d3, second_function);
    

    Then, you can access it like this:

    first_function( paramForThirdFunc1,paramForThirdFunc2);