title: How to create object member variable that contains a function pointer
body: <p>In Javascript, I have an object that contains a member variable called "dialogHost". It is itself an object that can call different functions. Currently, the way I have it set up is that in most cases, the object's dialogHost value is undefined. In the rare cases where it needs to be defined, I define it to be something like "window.parent" for example. The problem with setting it up this way is that any time I need to use this member variable, I have to check whether it is defined first, like:</p>
  
  <pre><code>var myDelegate;
  
  if (typeof (myObject.dialogHost) !== 'undefined') {
      myDelegate = myObject.dialogHost.someFunction;
  } else {
      myDelegate = someFunction;
  }
  
  myDelegate(...);
  </code></pre>
  
  <p>I have to do this type of check in a few places and it would be nice if the value always existed. Should I simply provide a default value of "window" for dialogHost in this case? I'm not sure if that is a good idea or not. Basically, I want the default value to be such that I can simply call:</p>
  
  <pre><code>// This would be equivalent to myDelegate(...) above...
  myObject.dialogHost.someFunction(...);
  </code></pre>
  
  <p>Without having to do the above checks every time. Is there a way to do this?</p>