javascript - JS: How to use abbreviations for full named variables? -


i prefer declare global variables @ beginning of code using self-explanatory, full-named variables:

var $interval_start_dates          = [];    // date intervals var $interval_end_dates            = [];  var $interval_start_milli          = [];    // convert dates milliseconds var $interval_end_milli            = [];  var $interval_start_milli_sorted   = [];    // sort millisecond dates var $interval_end_milli_sorted     = []; 

this naming convention results in long variable-names find unhandy when using them in code. therefore prefer use abbreviations variables in code:

var _isd  = $interval_start_dates; var _ied  = $interval_end_dates;   var _ism  = $interval_start_milli;  var _iem  = $interval_end_milli;  var _isms = $interval_start_milli_sorted; var _iems = $interval_end_milli_sorted; 

my questions in regard following:

(1) possible output intermediate variable (e.g. $interval_start_dates) using abbreviation (e.g. _isd)?

(2) naming convention result in worse performance of code (e.g. speed) , if there better way use abbreviations?

one remark: know use comments inform full name of abbreviated variable. means have repeat comments many times in code. looking more "slick" solution allows me use example console.log display full name of variable (if possible).

1) possible output intermediate variable (e.g. $interval_start_dates) using abbreviation (e.g. _isd)?

yes. both refer same array, doesn't matter use output array.

(2) naming convention result in worse performance of code (e.g. speed) , if there better way use abbreviations?

no. both refer same array, each direct reference other.

but, there other potential issues. instance, if had need of filtering or mapping arrays, easy update 1 reference , forget other, e.g.:

_isd = _isd.filter(function(entry) { return entry.foo < bar; }); // ooops! `_isd` , `$interval_start_dates` don't refer same array anymore! 

if it's typing out names, ide can make easier you.

i looking more "slick" solution allows me use example console.log display full name of variable (if possible).

i'm not sure you're asking here, if mean want use _isd somewhere (for instance, function) , have function display $interval_start_dates in output instead of _isd, can't (reasonably) that. there no link between _isd , $interval_start_dates, other refer same array. then, can't anyway — if passed $interval_start_dates function, still can't infer name of it, because function's argument receives value isn't linked in way $interval_start_dates (other refer same array).


side note: if of these things arrays, , want have way of knowing "name" of array based on array reference, can take advantage of fact standard arrays in javascript aren't arrays @ all, they're objects , assign them property name in, instance:

var $interval_start_dates          = [];    // date intervals $interval_start_dates.name = "$interval_start_dates";  // ...  var _isd = $interval_start_dates; console.log(_isd.name); // "$interval_start_dates" 

if that, sure you're not using for-in loop through arrays (but there better ways anyhow). or use object.defineproperty define name instead:

var $interval_start_dates          = [];    // date intervals object.defineproperty($interval_start_dates, "name", {     value: "$interval_start_dates" }); 

...and properties added way non-enumerable default.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -