The accessibility boundary of variables in JavaScript starts from the point where they are declared to every function invoked from that point including callbacks and either setTimeout() or setInterval().
If you have to define an instantiable class you may use the definition of the dynamic class called MainClass in this example:
function MainClass () { // ------------------------------------ Private attributes & methods var me = this, privateVar = 1; function init() { bindEvents(); } function bindEvents() { $('.aSelector').on('click', ClickOn_aSelector); } function ClickOn_aSelector(e) { e.preventDefault(); privateFunc(); me.publicMethod(); // "me." allows to access public methods or attributes // from either private or public methods. } function privateFunc () { console.log(privateVar); console.log(me.publicAtt); } // ------------------------------------ Public attributes & methods this.publicAtt = 2; this.publicMethod = function () { var _myself = this; privateFunc(); _myself.publicMethod2(3) }; this.publicMethod2 = function (pNum) { console.log (privateVar + this.publicAtt + pNum); // 6 }; // init(); }; var oMainClass = new MainClass(); oMainClass.publicMethod();
No comments:
Post a Comment