Note that there are some explanatory texts on larger screens.

plurals
  1. PONesting functions in nodejs / javascript
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>Example:</p> <pre><code>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 ); } }); } }); } }); }; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload