Jesse McCall
Jesse McCall

Reputation: 53

Where does this javascript function get its data?

I'm relatively new to javascript. Right now, I'm trying to wrap my head around embedded functions and the syntax looks strange (I keep on incorrectly trying to rely on my Java knowledge to figure it out) but I'm getting the hang of it.

I do have one hangup, thus far. I'm looking at working code that has a function very similar to this:

    function test(){
            return function(d, i) {
               ...
            };
    }

My question is, where in the world does the data pertaining to the return function come from? I understand that data in an embedded function can be passed from its encapsulating parent, but it looks as though the parent function has no data to actually pass considering it has no parameters of its own!

As a note, I looked through all of the other "Questions that may already have your answer" and couldn't find anything that related to my problem. I saw a few that were close but none that hit the nail on its head. A lot of those had a parent/encompassing function that had parameters of its own.

Upvotes: 0

Views: 46

Answers (1)

Dan D
Dan D

Reputation: 2523

Your function test() is returning a function, not executing. So the parameters would be provided by whoever invokes the return function.

function test() {
    return function(d, i) {
        ...
    };
}    

var innerFunc = test();

var result = innerFunc(1, 2);  // or call or apply

Upvotes: 1

Related Questions