Friday, February 23, 2018

Function Closure


A function closure contains a function and the environment the function is referenced in. The referencing environment may contain variables that are not local to the function but are nonetheless accessible to the function code. These non-local variables are called free variables or upvalues.

// example of function closure for Inner()
function Outer(){
    var upvalue = 1

    function Inner(){
        // upvalue is available here & part of function closure
        // for Inner()
        var funcVar = 2
    }
}