Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>overloadSetter</strong></p> <p><code>overloadSetter</code>, together with <code>overloadGetter</code>, are two function decorator methods. The <code>overloadSetter</code> function is used to transform functions that have the signature <code>fn(key, value)</code> to functions that could accept object arguments, ie: <code>fn({key: value})</code>.</p> <p>In order to do this, <code>overloadSetter</code> must wrap the original function. This wrapper function has the signature <code>fn(a, b)</code> which is a shortcut for <code>fn(key, value)</code>. This effectively becomes the new overloaded version of the original function.</p> <p>First thing this overloaded function does is check whether the passed <code>key</code> argument (<code>a</code>) is of the string type or not. If it's not a string, the function assumes that we're passing an object. So it iterates over each key-value pair in the object and applies the original function to it. If it's a string, on the other hand, it simply applies the function to the values of the <code>a</code> and <code>b</code> arguments.</p> <p><strong>Example</strong></p> <p>To illustrate, let's say we have the following function:</p> <pre><code>var fnOrig = function(key, value){ console.log(key + ': ' + value); }; var fnOver = fnOrig.overloadSetter(); fnOver('fruit', 'banana'); fnOver({'fruit': 'banana', 'vegetable': 'carrot'}); </code></pre> <p>In the first invocation, the <code>fnOver</code> function is invoked with two arguments, a key and a value. When the function checks the type of the <code>a</code> argument value, it'll see that it is a string. Therefore, it will simply invoke the original <code>fnOrig</code> function: <code>fnOrig.call(this, 'fruit', 'banana')</code>. Our console output is <code>'fruit: banana'</code>.</p> <p>For the second invocation, the <code>fnOver</code> function is invoked with an object argument. Since we passed an object instead of a string, <code>fnOver</code> will iterate through the members of this object and invoke the <code>fnOrig</code> function for each one of them. Thus, <code>fnOrig</code> will be invoked twice in this case: <code>fnOrig.call(this, 'fruit', 'banana')</code> and <code>fnOrig.call(this, 'vegetable', 'carrot')</code>. Our console output is <code>'fruit: banana'</code> and <code>'vegetable: carrot'</code>.</p> <p><strong>Extras</strong></p> <p>Inside the wrapper function, you'll see that there's an check for the value of <code>usePlural</code>. This is an argument for the <code>overloadSetter</code> method itself. If you set this value to <code>true</code>, the new function will treat all arguments as object. This means that even if you pass a string key argument, it will still be processed as an object.</p> <p>The other thing, the <code>enumerables</code> code that preludes the actual method declaration, is there because it fixes an issue with some browsers wherein the native <code>Object</code> methods are not enumerated in <code>for/in</code> loops even if the object itself implements its own version of it.</p>
    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. 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