Function referencing via variable definition

A defined variable that references a function previously defined ignores the original parameters passed into the reference/call. When you then call said defined variable and pass in another set of parameters, the referenced function's "return" definition will ignore the original parameters passed to said function at the time of variable definition. BIG QUESTION/LESSON: When a function is called directly, returning an anonymous function will not receive any parameters. However, when a function is called through a reference such as var inc = funcName();, the "return" function definition will THEN receive the parameters passed to inc().

by lasha

JavaScript

function makeIncrementer(a,b,c,d,e) {
    console.log(a,b,c,d,e); // this accesses the 5 parameters passed when defining "var inc" below. We can also reference those parameters by doing makeIncrementer(a,b,c,d,e) and use them within the return function below.
    
    return function(x,y,a) { 
        console.log(x,y,a); // 3rd parameter returns undefined because 3rd parameter is not defined in the inc() call below. If you remove 3rd paramter, console.log will reference "a" from outer context.
        return arguments[0] + 1; // same as "x + 1"
        // This return function, by default, invisibly accesses the parameters passed to inc() below... and IGNORES the 5 parameters passed when defining "var inc"
        // instead of using "arguments", we can pass actual paramer names to the return function, such as "function(val1,val2)" instead of leaving is blank like it is right now, and refer to them that way, instead of arguments[0] and argument[1]
    };

}

var inc = makeIncrementer(1,2,"3",4,"five");

// displays 9
alert(inc(8,"something")); // var inc is a reference to the makeIncrementer() function. Parameters are passed to inc(), which then get passed to makeIncrementer().