/ Published in: Other

Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
var myModule = function () { var publicObj = {}; // returnable object... var privateProperty = "I can be accessed only from within this module"; var privateMethod = function () { // has access to public properties as publicObj.publicVar and private methods } var publicObj.publicProperty = "Publicly accessible as myModule.publicVar."; var publicObj.publicMethod = function () { // Publicly accessible as myModule.publicMethod } return publicObj; }(); var myModule = function(){ var privateMethod = function(){ publicMethod(); } var publicMethod = function(){ return this; } // Copy over methods you want to be public return { publicMethod : publicMethod } }(); var myModule = function(){ // Persists in privateMethods due to closure var that = {}; function privateMethod(){ that.publicMethod(); } return { publicProperty: true, publicMethod: function(){}, // Make this visible in private scope init: function(){ that = this; } }; }().init();
Comments
