From what I've read, nesting functions in javascript causes extra declarations / destructions which can be avoided by using "static functions" or even a Singleton implementation. Also "new" does the same thing where two instances of the function or objects are also independent copies.
Is this true? If so, what can I do to have the same functionality as with nested functions and with "new". This is for a game where the server is in nodejs / javascript. I've reached about 8 levels of nested functions and am starting to worry.
Example:
DB.cityUpdateUpkeep = function( cuid )
{
/** @type {Array} */
var buildings = null;
DB.cityGet( cuid, function( error, city )
{
if( error )
{
console.log( "Couldn't get city" );
}
else
{
DB.iBuildings.find( {cuid:cuid}, function( error, cursor )
{
if( error )
{
console.log( "-error:" );
console.log( error );
}
else
{
cursor.toArray( function( error, response )
{
if( error )
{
console.log( "-error:" );
console.log( error );
}
else
{
console.log( "-the response:" );
console.log( response );
buildings = response;
var income = city.resources.income;
var storage = city.resources.storage;
var stored = city.resources.stored;
for( var buildingID in buildings )
{
var building = buildings[ buildingID ];
var blueprint = DB.bBuildings[ building.buid ];
if( blueprint.resources.income )
{
income = Utils.sumObjects( income, blueprint.resources.income );
}
if( blueprint.resources.storage )
{
storage = Utils.sumObjects( storage, blueprint.resources.storage );
}
if( blueprint.resources.stored )
{
stored = Utils.sumObjects( stored, blueprint.resources.stored );
}
}
console.log( "cuid: " + city._id + " income: " + income + " storage " + storage + " stored " + stored );
}
});
}
});
}
});
};