setting a default value to an undefined variable in javascript
问题 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