Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply" rel="nofollow"><code>Function.apply</code></a> can only be used to apply an <em>array-like</em> object<sup>1</sup> to a function call. There is no equivalent to Pythons "keyword argument expansion" and this must be done manually:</p> <pre><code>var opts = { dos: 'foo', tres: 'bar', uno: 'baz' } doSomething(opts.uno, opts.dos, opts.tres) </code></pre> <p>If we started with an <em>array-like</em> object:</p> <pre><code>var arr = ['baz', 'foo', 'bar'] doSomething.apply(window, arr) </code></pre> <p>Or putting the two together (so that the composition of to a parameter sequence can be handled earlier):</p> <pre><code>var arr = [opts.uno, opts.dos, opts.tres] doSomething.apply(window, arr) </code></pre> <p>While it <em>may</em> be possible (with non-obfuscated code) to use <a href="http://es5.github.io/#x15.3.4.2" rel="nofollow"><code>Function.toString</code></a><sup>2</sup> to extract the parameter names, do some parsing, and then write some code to "apply" a general object (or construct an array that is then applied), this is not supported directly by the ECMAScript specification.</p> <hr> <p><sup>1</sup> An <em>array-like</em> object is one that has a <code>length</code> and properties named <code>0..length-1</code>. (Old versions of FF had a bug, but that has been fixed for a long time.)</p> <p><sup>2</sup> The wording of the specification ("An implementation-dependent representation of the function is returned. This representation has the syntax of a FunctionDeclaration..") makes me believe that a conforming ES5 implementation <em>ought to</em> produce output usable for this purpose - however, this will likely vary by implementation and I have not explored such usage in practice.</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.
    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