I’ve been looking up JavaScript patterns to get started on. I know enough JavaScript to be dangerous, but not enough to be a good architect.
I started by using the module pattern to create a “Model” for managing some Interest data. With the code below, how can I get a “constructor” method (or some sort of init method to fire) to initalize data when it’s used. This data would be used across all instances, so I’m guessing I would use prototype?
var InterestDataModel = (function () {
// Private
var interestModels = [];
return {
getInterestModels: function () {
return interestModels;
},
resetInterestModels: function () {
interestModels.length = 0;
}
}; // end return
})();
// Calling code
// When this call is made, I want some initialization to be done already!
InterestDataModel.getInterestModels();
Since you’re self executing the ‘InterestDataModel’ function anything inside of that function will be the initialization and run before you call the getInterestModels method. See this jsfiddle example: http://jsfiddle.net/rjrqb/1/