title: Javascript execute optional function of object
body: <p>If I have an object like this</p>
  <pre><code> const obj = {
       func: props.func, // we don't know if func is a function or undefined...
   }
  </code></pre>
  <p>and I have this in my code:</p>
  <pre><code> obj.func(params);
  </code></pre>
  <p>What is the good way to execute the optional function of the object?</p>
  <p>I have thought to do something like this:</p>
  <pre><code>if(typeof obj.func === &quot;function&quot;)
     obj.func(params);
  </code></pre>
  <p>or</p>
  <pre><code>try {
      obj.func(params);
  } catch (err) {}
  </code></pre>
  <p>But I'm not sure if both forms are correct to do it (or if there is a better way) as I don't have much experience with javascript.</p>
  <p>Someone who knows? Thank you.</p>