VivekDev
VivekDev

Reputation: 25417

Js functions enclosed in parenthesis without invoking parenthesis

As far as I know IIFEs look like this.

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

Or may be

(function () { 
    ... 
}())

But I have a js which looks something like the following.

Are these functions IIFEs? Who is calling them? What is invoking them? I am able to place debugger and debug the methods.

(function (definition) {
    "use strict";

    if (typeof exports === "object" && typeof module === "object") {
        module.exports = definition( require('./chartiq') );
    } else if (typeof define === "function" && define.amd) {
        define(["chartiq"], definition);
    } else if (typeof window !== "undefined" || typeof self !== "undefined") {
        var global = typeof window !== "undefined" ? window : self;
        definition(global);
    } else {
        throw new Error("Only CommonJS, RequireJS, and <script> tags supported for quoteFeedSimulator.js.");
    }
})

(function(_exports){
  var CIQ=_exports.CIQ;
    var quoteFeedSimulator=_exports.quoteFeedSimulator={}; // the quotefeed object

    quoteFeedSimulator.generateGUID=function(){
            .......
    };

    quoteFeedSimulator.maxTicks=50000;

    // called by chart to fetch initial data
    quoteFeedSimulator.fetchInitialData=function (symbol, suggestedStartDate, suggestedEndDate, params, cb) {
            .......
    };

    // called by chart to fetch update data
    quoteFeedSimulator.fetchUpdateData=function (symbol, startDate, params, cb) {
            .......
    };

    // called by chart to fetch pagination data
    quoteFeedSimulator.fetchPaginationData=function (symbol, suggestedStartDate, endDate, params, cb) {
            .......
    };

    // utility function to format data for chart input; given simulator was designed to work with library, very little formatting is needed
    quoteFeedSimulator.formatChartData=function (response) {
        var feeddata=JSON.parse(response);
        var newQuotes=[];
            .......
        return newQuotes;
    };

    return _exports;

});

Upvotes: 0

Views: 139

Answers (3)

Kit Isaev
Kit Isaev

Reputation: 700

In the above code, the first anonymous function:

function (definition) {
  ...
}

accepts the second anonymous function:

function (_exports) {
  ...
}

as an argument. The second function is then called from within the body of the first function with var global from the local scope of the first function as an argument. A simplified version of what is happening:

// The following code will log 'hey'

(function(defenition) {
   const global = 'hey';
   definition(global);
)(function(_exports) {
   console.log(_exports);
});

Upvotes: 1

biswajit-rout
biswajit-rout

Reputation: 377

IIFEs - Immediately Invoked Function Expressions. Its means when we need we can invoke a function inside a function expression. Its same as we are using callbacks.

That means we are invoking a function inside another function as a parameter, so that we can use that parameterised function inside it. Here is the example.

//This is my callback function/ IIFE
function foo(param){
  console.log("I am from foo - "+ param);
}

//I am creating another function who take foo function as a parameter.
(function(iamfoo){
  iamfoo(1); //foo will call with param 1
  console.log("I am calling foo"); // print this message
  iamfoo(2); //  agian foo will call with param 2
})(foo); // foo as a parameter

Output will be:

I am from foo - 1
I am calling foo
I am from foo - 2

I hope I clarified your doubt some how. Thanks and let me know your query on this.

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386624

Basically it is an IIFE, where the function's parameters is a function itself.

The handed over function is definition, which keeps the function which is later called.

(function (definition) {

})(function(_exports) { /* ... */ });

Upvotes: 2

Related Questions