Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can clone an array by calling <code>slice</code> directly:</p> <pre><code>var arr2 = arr1.slice(); </code></pre> <p>If you want a <code>clone</code> function, you can do:</p> <pre><code>var clone = function(arr) { return arr.slice(); }; </code></pre> <p>If you really want to prototype function (which is not necessary as long as the function is not overwritten):</p> <pre><code>var clone = function(arr) { return [].slice.call(arr); }; </code></pre> <hr> <p><strong>Why can't you reference <code>call</code> or <code>apply</code> directly?</strong></p> <p>It does not work for the same reason assigning a method of an object to a variable does not "work". </p> <p>If you call <code>func.call()</code> then <code>this</code> inside <code>call</code> will be a reference to <code>func</code>, a function object.</p> <p>If you assign <code>call</code> to a variable then the context is lost. You have a reference to the generic <code>call</code> function. Thus you'd have to pass the correct context (the method you want to apply <code>call</code> to) as first parameter to <code>call</code> again:</p> <pre><code>var clone = [].slice.call; var arr2 = clone.call([].slice, arr1); </code></pre> <p>This is not really an improvement and quite confusing.</p> <p><code>call</code> and <code>apply</code> are methods that every functions inherits from <code>Function.prototype</code>. Functions don't have their own version of them. <code>[].slice.call === [].splice.call</code> yields <code>true</code>.</p>
    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. 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