"Closure is the most important concept ever invented in computing science" - Kyle Simpson

This might clarify it a bit

let obj = {};

(function(){
    function fullName() { 
        return this.firstName + " " + this.surname; 
    }

    this.personFactory = function(firstName, surname){
        return { firstName, surname, fullName }
    }
}).call(obj);

let p = obj.personFactory("John","Smith");

It's a closure because the definition of fullName ought not to extend beyond the outer function call. That is, you can't call obj.fullName(), or just fullName(). Yet we can call it, by virtue of it being returned from the outer (anonymous) function (via the object).

The closure part in the example above is really the link between the object and the function - I just didn't want to over-complicate it - it was just a rough example.

/r/learnjavascript Thread Parent