Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, you need to not reassign the <code>options</code> argument to a var. Second, be careful with this for certain values:</p> <pre><code>var foo = function (options) { options = options || {}; var bar = options.bar || 42; var baz = options.baz || 'fubar'; }; foo({ bar: 0, baz: "" }); </code></pre> <p>Inside <code>foo</code>, <code>bar == 42</code> &amp; <code>baz == 'fubar'</code> because <code>0</code> and <code>""</code> are falsy. It's probably better to be more verbose so as to be more precise:</p> <pre><code>var foo = function (options) { options = options || {}; var bar = typeof options.bar == 'undefined' ? 42 : options.bar; var baz = typeof options.baz == 'undefined' ? 'fubar' : options.baz; }; </code></pre> <p>But, to be DRY, you can create a <code>defaults</code> object and just extend both objects to a <code>settings</code> object:</p> <pre><code>// generic shallow object extension, mutates obj1 var extend = function (obj1, obj2) { for (var prop in obj2) { if (obj2.hasOwnProperty(prop)) { obj1[prop] = obj2[prop]; } } return obj1; }; var foo = function (options) { var defaults = { bar: 42, baz: 'fubar' }, settings = extend(defaults, options); }; </code></pre> <p>Since you won't be using <code>defaults</code> any more in this invocation it doesn't matter that it gets mutated. Using this method:</p> <pre><code>foo(); // -&gt; settings = { bar: 42, baz: 'fubar' } foo({ bar: 0 }); // -&gt; settings = { bar: 0, baz: 'fubar' } foo({ bar: 0, baz: "" }); // -&gt; settings = { bar: 0, baz: '' } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload