title: Handling undefined fields when creating a custom javascript object
body: <p>I've always created javascript objects in the following way:</p>
  
  <pre><code>//object code
  function someObject() {
      this.field1;
      this.fiend2;
  }
  
  function makeSomeObject(data) {
      var result = new someObject();  
      result.field1 = data.field1DataName;
      result.fiend2 = data.field2DataName;
      return result;
  }
  
  //object instantiation
  var someObject = makeSomeObject(data);
  processObject(someObject);
  </code></pre>
  
  <p>I now want to do this more defensively. In other words I want to test data.field1DataName before setting it. I'm attempting to do this in the following way.</p>
  
  <pre><code>//object code
  function someObject() {
      this.field1;
      this.fiend2;
  }
  
  function makeSomeObject(data){
      var result = new someObject(); 
      result.field1 = typeof data != "undefined"  ? "field1DataNameUndefined": data.field1DataName;
      result.field2 = typeof data != "undefined"  ? "field2DataNameUndefined": data.field2DataName;
      return result;
  }
  
  //object instantiation
  var someObject = makeSomeObject(data);
  processObject(someObject);
  </code></pre>
  
  <p>Now if a data field is missing that will be caught before the object field is set. The problem is that the <code>someObject</code> still gets created and <code>processObject</code> will accept it with out knowing there a problem. My question is what is the best practice for indicating that there's a problem with <code>someObject</code> so I can avoid problems in <code>processObject</code>. If anyone has any suggestions for a better practices for defensive programming in javascript in the course of considering this issue that feedback is appreciated as well.</p>