I want to rewrite a file to incorporate public and private vars. I am trying to do something like so:
var PrivateFunc = (function() {
//Private Functions
var _iAmPrivate = function(param, args){
//code
}
var _soAmI = function(param){
//code
}
return PrivateFunc;
})();
var PubObj = {
someVar: 1,
getPrivFunc: PrivateFunc,
publicFunc: function(param){
//This doesnt work
this.getPrivFunc._iAmPrivate(someVar, param);
//This doesn't either
getPrivFunc._soAmI(param);
}
};
I am a "getPrivFunc is undefined" error. I thought that moving getPrivFunc outside of PubObj could work, but i ran into the same issue. Calling PrivateFunc give errors as well. I feel like it is a silly error, I am just not seeing it.
Maybe there is a better way to do this?
PrivateFuncisundefined. What you actually do is assigning the variable to itself (return PrivateFunc;). It's the same asvar foo = foo;. But sincefoo(PrivateFunc) was never assigned a different value, its value will beundefined.