How to correctly describe a Javascript closure -


looking @ implementation of 'singleton' in javascript (something i've been playing please correct me if i'm wrong), closure?

as far understand reading "you don't know js" book on scope , closures, correct terminology say: function (or module) m1 has closure on main scope.

to describe this, m1 creates private scope within main object. conceptual boundary between these 2 scopes considered 'closure'.

but seem me in order closure (and not function private scope), need interface accessible within scope 'closing off' gives access private scope.

i.e. seem me closure defined making variables within private scope available respective 'public' scope after control has been handed 'public' scope after executing function creating closure.

is above correct?

/**  * singleton object implementation  */ var m1 = (function() {      // private variables     var items = [];     var x = 5;      // getters , setters     var getx = function() {return x;};     var getitems = function() {return items;};      // private functions     var item = function(name) {         this.name = name;     };      // public interface     return {         getx: getx,         getitems: getitems,         createitem: function(name) {             items.push(new item(name));         }     }; })(); 


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -