node.js - Nesting functions in nodejs / javascript -
from i've read, nesting functions in javascript causes declarations / destructions can avoided using "static functions" or singleton implementation. "new" same thing 2 instances of function or objects independent copies.
is true? if so, can have same functionality nested functions , "new". game server in nodejs / javascript. i've reached 8 levels of nested functions , starting worry.
example:
db.cityupdateupkeep = function( cuid ) { /** @type {array} */ var buildings = null; db.cityget( cuid, function( error, city ) { if( error ) { console.log( "couldn't 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 ); } }); } }); } }); };
take @ q ways flatten callbacks (the code little nicer). specific example, prefer refactor using number of methods:
- return methods when err (saves having nest else's)
- execute asynchronous calls concurrently when they're not dependent (notice db.cityget , db.ibuildings.find - makes code run faster well)
- creating functions , referencing outside nest (ex. checkcomplete)
my refactor below:
db.cityupdateupkeep = function( cuid ){ /** @type {array} */ var buildings = null; var city = null; var checkcomplete = function(){ if (!city || !builings){ return; } 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 ); } db.cityget(cuid, function(err, response){ if (err){ console.log("couldn't city"); return; } city = response; checkcomplete(); }); db.ibuildings.find({cuid:cuid}, function(err, cursor){ if (err){ console.log(err); return; } cursor.toarray(function(err, response){ if (err){ console.log(err) return; } buildings = response; checkcomplete(); }); }); });
Comments
Post a Comment