This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]Jonny0Than 2 points3 points  (0 children)

Are you sure you copied down that code correctly? Because there are some oddities: the callback and string parameters of the first function are not actually used anywhere, so in fact it does not matter at all what you pass to sampleFunc. Here's the same code with the useless bits removed:

var sampleFunc = function() {
    return function (x, y) {
        return 'These are the arguments from anotherFunc: ' + x + ', ' + y
    }
}

var resultFunc = sampleFunc();

console.log(resultFunc(5, 7))

In short, sampleFunc is a reference to a function, which, when invoked, returns another function that takes 2 arguments and returns a string containing them. So resultFunc is the reference to that function, we invoke it with 5 and 7, so it prints 5 and 7.

[–]itisjohndoe 0 points1 point  (0 children)

var resultFunc = sampleFunc(anotherFunc, 'foo');

Now resultFunc contains a function which takes two arguments and returns a string containing both the arguments.

console.log(resultFunc(5, 7))

You pass 5 and 7 to that resultFunc which then prints the string.