Can someone explain why this occurs in Javascript?
var singleton = window.singleton || {};
singleton.methods = (function () {
var _private = function() {
console.log('outer private function');
}
return {
_private: function() {
console.log('inner private');
},
public: function() {
_private();
}
}
})();
singleton.methods.public();
My intuition leads me to believe that calling .public()
should log 'inner private'. It doesn't. If I change the public()
definition to this._private()
I get what I would expect. Why?