I'm trying to have 2 classes (factorys) in my Module
angular.module('starter.services', [])
.factory("TPreferences",function($cordovaSQLite){
_set = function(pName, pValue){
console.log("Setting: "+pName+":"+pValue);
}
_addList = function(pList){
for(var p=0; p < pList.length; p++)
{
_set(pList[p].name, pList[p].value);
}
}
return {
init: function(){
_addList(gSettings);
}
}
})
.factory("TDayList",function($cordovaSQLite){
_add = function(pName, pValue){
console.log("Day: "+pName+":"+pValue);
}
_addList = function(pList){
for(var p=0; p < pList.length; p++)
{
_add(pList[p].name,pList[p].value);
}
}
return {
init: function(){
_addList(gExampleDays);
}
};
});
Problem: When calling TPreferences.init(), "Day:..." is logged. I assumed that ".factory(...)"-Stuff to be a class, where _addList is a protected function, but it seems that _addList seems to be (at least module-wide) global and is overwritten by the second definition with the same name even though in a different factory.
Question: Sure, I could in this example simply change the name of one of the functions, but I would more like to handle a factory like a class, so that I can use names of internal functions twice. So, what would be the smartest way to do so?
but it seems that _addList seems to be (at least module-wide) global and is overwritten by the second definition
Of course it's global, because you declared it globally. Initializations without declarations become global variables. So never forget declaring your variables properly (const
, let
, var
):
const _addList = function(pList) {
for (var p=0; p < pList.length; p++) {
_set(pList[p].name, pList[p].value);
}
}