/ Published in: JavaScript

Simple illustration of structuring your JavaScript and minimising your use of global vars.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
var myModule = function(){ //Keep the vars outside the global namespace (global vars are evil!) var a = 122; var b = 22; return { init: function(){ console.info("The module has been initialised"); }, add: function(c){ //c doesn't exhist so 0; c = c || 0; var sumA = a + c; var sumB = b + c; console.log(sumA); console.log(sumB); } } }(); myModule.init();//Initialise myModule.add(33);
Comments
