closure: to close over or not

JSvaluefunctionclosureexample ⟩ to close over or not

even an outer variable is not mentioned explicitly in a closure, it is still being closed over by the closure

// • returns a closure
// • parameters are closed over
function GetInfo(id, name, grade) {
    // ⭐ closure
    // although `id`, `name`, `grade` are not mentioned explicitly,
    // these parameters are still being closed over by the closure.
    return function getInfo(whichValue) {
        // ⭐ eval('id') returns `id`, etc ...
        return eval(whichValue);
    };
}

const info = GetInfo(73, "Suzy", 87);
info("name"),    // Suzy
info("grade"),   // 87

Last updated