Reputation: 635
I was reading about closures and i came across with this example:
var counter = (function() {
var privateCounter = 0;
function changeBy(val) {
privateCounter += val;
}
return {
increment: function() {
changeBy(1);
}
};
})();
So basically a closure is :
A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment).
Ok so far so good. But in following example, we dont have functions bundled together but the returned object keeps a ref to privateCounter.
Can we consider this also some sort of closure?
var counter = (function() {
var privateCounter = { name: "nick" };
return {
name: privateCounter
};
})();
Upvotes: 0
Views: 34
Reputation: 138257
No. The function does not access variables from it's outer scope.
Upvotes: 1