Suppouse the next example extracted from another OLOO question:
var Foo = {
init: function(who) {
this.initialized = true;
this.me = who;
},
checkIfInitialized: function() {
if(!this.initialized) throw new Error('Not initialized')
},
identify: function() {
checkIfInitialized()
return "I am " + this.me;
},
sayHi: function() {
checkIfInitialized()
return "Hi " + this.me;
},
sayGoodBye: function() {
checkIfInitialized()
return "Goodbye " + this.me;
}
};
Here, I want to throw an error or deny the call of identify if the object was not initialized before call the function. However, I dont want to add a verifier inside each public routine of the Object.
How can I handle it without duplicating that behaviour?