Ideally I want to be able to write something like:
function a( b ) {
b.defaultVal( 1 );
return b;
}
The intention of this is that if b
is any defined value, b
will remain as that value; but if b
is undefined, then b
will be set to the value specified in the parameter of defaultVal()
, in this case 1
.
Is this even possible?
Ive been toying about with something like this:
String.prototype.defaultVal=function(valOnUndefined){
if(typeof this==='undefined'){
return valOnUndefined;
}else{
return this;
}
};
But I'm not having any success applying this kind of logic to any variable, particularly undefined variables.
Does anyone have any thoughts on this? Can it be done? Or am I barking up the wrong tree?