Closures ?

A closure is an inner function that has access to the outer (enclosing) function’s variables—scope chain. The closure has three scope chains: it has access to its own scope , it has access to the outer function’s variables, and it has access to the global variables.

Let’s imagine the situation: a driver in his car, is parked inside a jumbo jet which is currently parked on a runway in an airport. The driver is capable of accessing anything within his car, and he has the ability to get out of his car and access parts of the plane. However, the driver is unable to access the airport.

Here is how I can convert my plane story into the code.

    var plane = function (defaultAirport) {

    var lastAirportLeft = defaultAirport;

    var car = {
        driver: {
            startAccessPlaneInfo: function () {
                setInterval(function () {
                    console.log("Last airport was " + lastAirportLeft);
                }, 2000);

    return {
        leaveTheAirport: function (airPortName) {
            lastAirportLeft = airPortName;
}("Boryspil International Airport");

plane.leaveTheAirport("John F. Kennedy");

More Info : MDN


My Name is Dileep Singh, Noder, Javascript Lover & NoSql Developer, Fitness Freak, Love to travel new places, learning photography & Music Manic ♯ ♩ ♬

Posted in Javascript Tagged with: