Quick actions

cmd+k|ctrl+k

Navigation

Languages

Closures

Snippet info

Language

JavaScript

Visibility

public

Author

apizzimenti

Created

2016-03-19T06:09:52Z

Updated

2016-03-20T19:32:39Z

/*
Closure functions are functions that refer to the state of their enclosing
function's scope (this includes the 'this' keyword). Any free variable in the
closure function that is declared in the outer function's scope or received as
a parameter.
*/

function parent(param) {
    return function () {
        console.log(param);
    };
}

var a = parent('here');
a();

/*
This behavior can also be chained.
*/

function parent_2(param) {
    function getter() {
        return function () {
            console.log(param);
        };
    }
    return getter;
}

var b = parent_2(2);
var c = b();
c();

INFO