Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Others have already explained what this code does. It creates an object (called <code>this.String</code>) that contains a single function (called <code>Get</code>). I'd like to explain when you could use this function.</p> <p>This function can be useful in cases where you need a higher order function (that is a function that expects another function as its argument).</p> <p>Say you have a function that does something to each element of an <code>Array</code>, lets call it <code>map</code>. You could use this function like so:</p> <pre><code>function inc (x) { return x + 1; } var arr = [1, 2, 3]; var newArr = arr.map(inc); </code></pre> <p>What the <code>map</code> function will do, is create a new array containing the values <code>[2, 3, 4]</code>. It will do this by calling the function <code>inc</code> with each element of the array.</p> <p>Now, if you use this method a lot, you might continuously be calling <code>map</code> with all sorts of arguments:</p> <pre><code>arr.map(inc); // to increase each element arr.map(even); // to create a list of booleans (even or odd) arr.map(toString); // to create a list of strings </code></pre> <p>If for some reason you'd want to replace the entire array with the same string (but keeping the array of the same size), you could call it like so:</p> <pre><code>arr.map(this.String.Get("my String")); </code></pre> <p>This will create a new array of the same size as <code>arr</code>, but just containing the string <code>"my String"</code> over and over again.</p> <p>Note that in some languages, this function is predefined and called <code>const</code> or <code>constant</code> (since it will always return the same value, each time you call it, no matter what its arguments are).</p> <hr> <p>Now, if you think that this example isn't very useful, I would agree with you. But there are cases, when programming with higher order functions, when this technique is used.</p> <p>For example, it can be useful if you have a tree you want to 'clear' of its values but keep the structure of the tree. You could do <code>tree.map(this.String.Get("default value"))</code> and get a whole new tree is created that has the exact same shape as the original, but none of its values.</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