Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>function read_file(file, delete_after) { delete_after = delete_after || "my default here"; //rest of code } </code></pre> <p>This assigns to <code>delete_after</code> the value of <code>delete_after</code> if it is not a <em>falsey</em> value otherwise it assigns the string <code>"my default here"</code>. For more detail, check out <a href="http://crockford.com/javascript/survey.html" rel="noreferrer">Doug Crockford's survey of the language and check out the section on Operators</a>.</p> <p>This approach does not work if you want to pass in a <em>falsey</em> value i.e. <code>false</code>, <code>null</code>, <code>undefined</code>, <code>0</code> or <code>""</code>. If you require <em>falsey</em> values to be passed in you would need to use the method in <a href="https://stackoverflow.com/questions/894860/how-do-i-make-a-default-value-for-a-parameter-to-a-javascript-function/894877#894877">Tom Ritter's answer</a>.</p> <p>When dealing with a number of parameters to a function, it is often useful to allow the consumer to pass the parameter arguments in an object and then <em>merge</em> these values with an object that contains the default values for the function</p> <pre><code>function read_file(values) { values = merge({ delete_after : "my default here" }, values || {}); // rest of code } // simple implementation based on $.extend() from jQuery function merge() { var obj, name, copy, target = arguments[0] || {}, i = 1, length = arguments.length; for (; i &lt; length; i++) { if ((obj = arguments[i]) != null) { for (name in obj) { copy = obj[name]; if (target === copy) { continue; } else if (copy !== undefined) { target[name] = copy; } } } } return target; }; </code></pre> <p>to use</p> <pre><code>// will use the default delete_after value read_file({ file: "my file" }); // will override default delete_after value read_file({ file: "my file", delete_after: "my value" }); </code></pre>
    singulars
    1. This table or related slice is empty.
    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. 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