code is being able to notice the recurring themes in that code and optimize them. This is an area where knowledge of design patterns can prove invaluable.”
a reference to the Singleton var instance; function init() { var privateRandomNumber = Math.random(); return { getRandomNumber: function() { return privateRandomNumber; } }; }; return { // Get the Singleton instance if one exists // or create one if it doesn't getInstance: function () { if ( !instance ) { instance = init(); } return instance; } }; })();
a view that users interact with var employeeDetail = this.getEmployeeDetail(); // when the employee detail is complete, the mediator (the 'orgchart' object) // decides what should happen next employeeDetail.on("complete", function(employee){ // set up additional objects that have additional events, which are used // by the mediator to do additional things var managerSelector = this.selectManager(employee); managerSelector.on("save", function(employee){ employee.save(); }); }); }, // ... }
& functions start with a lowercase letter but with modules, that is not the case. // The general tradition is to start them with a capital letter instead. function MyModule() { // `this` refers to the instance of `MyModule` when created this.myMethod = function myMethod() { alert( 'my method' ); }; // note that we still use a function declaration even when using a function expression. // for more information on why, check out: http://kangax.github.io/nfe/ this.myOtherMethod = function myOtherMethod() { alert( 'my other method' ); }; } // expose access to the constructor window.MyModule = MyModule; } )( window );