#Javascript Revealing Module Pattern

Oscar Brito



If you like to create Private methods like you do in Java, you can follow the Revealing Module Pattern. This pattern only expose the methods you declare. Take a look:



var module = (function() {

var auxiliar1 = function() {
};
var _auxiliar2 = function() {
};

var do
stuff1 = function() {
};

var dostuff2 = function() {
};

return {
do
stuff1 : dostuff1,
do
stuff2 : dostuff2
};

});

//run things
var obj1 = new module();
obj1.do
stuff1();
obj1.do_stuff2();


What happened is that the object was created using a self-executing anonymous function that returned an hash. Using the prototype to create objects you can't hide the private methods::



var module = function() {

/* not so much - private methods */
this.auxiliar1 = function() {
};

this.
auxiliar2 = function() {

};

}

module.prototype = {

dostuff1: function() {
},

do
stuff2: function() {
},

}

//run things
var obj1 = new module();
obj1.dostuff1();
obj1.do
stuff2();



LINK:
http://blog.alexanderdickson.com/javascript-revealing-module-pattern




Visit www.divhide.com for more informations, contacts and news about Web Development.
See other blog posts at blog.divhide.com.



Divhide purpose is to follow the HTML5 movement and contribute with applications which prove the quality of technology.


Feel free to contact divhide.