/ Published in: JavaScript
/*
* A little help function for when I'm creating functions
*
* When I pluck values from function arguments I tend to
* write content of this function repeatedly.
*
* e.g.
* a = arguments
* this.id = arg(a, 0)
* this.name = arg(a, 3)
*
* this.id == 6
* this.name == null
*/
* A little help function for when I'm creating functions
*
* When I pluck values from function arguments I tend to
* write content of this function repeatedly.
*
* e.g.
* a = arguments
* this.id = arg(a, 0)
* this.name = arg(a, 3)
*
* this.id == 6
* this.name == null
*/
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/* * A little help function for when I'm creating functions * * When I pluck values from function arguments I tend to * write content of this function repeatedly. * * e.g. * a = arguments * this.id = arg(a, 0) * this.name = arg(a, 3) * * this.id == 6 * this.name == null */ function arg(_a, ia, def, returnArray) { var v = null // if ia is an array, find the // first correct definition if (ia.constructor == Array) { /* * Each item is checked. if the * item in the array is * a definition within the oaet * arguments or object - pass it */ for(var i=0; i<ia.length; i++) { if(_a[ia[i]]){ v = _a[ia[i]]; break; } } } else { // if ia is just a value if(_a[ia]) v = _a[ia]; } if( (v == null) && (def != undefined) ) { v = def } if(returnArray){ return [v, ia[i]] } else { return v } }