Ideally, I want to write something like:
function a( b ) { b.defaultVal( 1 ); return b; }
The purpose of this is that if b is any specific value, b will remain that value; but if b undefined, then b will be set to the value specified in the defaultVal() parameter, in this case 1 .
Is it possible?
I was thinking of something like this:
String.prototype.defaultVal=function(valOnUndefined){ if(typeof this==='undefined'){ return valOnUndefined; }else{ return this; } };
But I did not succeed in applying this logic to any variable, especially to undefined variables.
Does anyone have any thoughts on this? It can be done? Or am I barking the wrong tree?
source share