Ok, so I a have a module called "SelectorsModule". In it, I have an object literal which will contain all the jQuery selectors that will be used within the application during various user interactions (see below)
In order to stop from repeating the same $(selector) code each time the selector is used, I thought it would be more efficient to place all shared selector elements into its own 'SelectorsModule'. This way, if any changes need to be made, it only needs to be modified in one place and everything would still work. Each selector could also then be called from a single place, ie. "SelectorsModule.getSelectors.selectorTwo", for example.
var SelectorsModule = SelectorsModule || {};
SelectorsModule = (function(){
// Private methods and variables
var sharedSelectors = {
selectorOne: $("#element-one"),
selectorTwo: $("#element-two"),
selectorThree: $("#element-three"),
selectorFour: $("#element-four")
};
// Public methods and variables
return{
getSelectors: function(){
return sharedSelectors;
}
};
})();
My questions:
Each time I use
SelectorsModule.getSelectors.selectorOnein my code now (to obtain the jQuery selector object), am I simply referencing the single instance of theSelectorsModule, created when the script is first run, or is a newSelectorsModule'object', created behind the scenes each time? The latter would obviously perform a lot worse and just need some clarification really?Is my current approach WRONG, FLAWED, OK, GOOD, PERFECTLY FINE or SPOT ON?