Reputation: 2311
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.
Upvotes: 1
Views: 374
Reputation: 707686
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.utils
is not yet defined.
There are alternatives. Here's one:
var app = {};
app.utils = {};
app.utils.Expiration = function(){ // contructor
... // some code
};
app.utils.Expiration.prototype = ...
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.
Upvotes: 5