Im building an app’s namespace and it looks as follows:
var app = {};
app.utils = {
Expiration: function(){ // contructor
... // some code
},
init: (function(){
app.utils.Expiration.prototype = function(){....}
())
};
But i get an error: TypeError: Cannot read property 'Expiration' of undefined, and actually it’s true beacuse utils is still being defined, i know that i can define the prototype outside of the scope of app, my question is: can i define it inside app or app.utils with an self executing function or by any other means, thanks.
You cannot refer to your own variable name in a static declaration. Your self executing function causes that to get evaluated and executed at the time of static declaration. Thus
app.utilsis not yet defined.There are alternatives. Here’s one:
Since each of these successive statements adds another level of properties, these statements have to get executed one after the other in this order so that the previous elements are in place.