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