Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It is a <a href="http://es5.github.com/#x13" rel="nofollow">Function Expression</a><sup>2</sup></p> <p>Function Expressions <em>can</em> have names as well; the names of Function Expressions are only in scope in the function body<sup>1</sup>. (Since ECMAScript 5th edition deprecates <code>arguments.callee</code>, this is the only simple way to write a recursive "anonymous" function.)</p> <p>Because it <em>is</em> a Function Expression, the name <em>may not</em><sup>1</sup> introduce a new binding in the outer scope.</p> <p>Also, <em>all</em> functions are objects in JavaScript. In <code>f(..)</code>, <code>f</code> is evaluated <em>before</em> it is "invoked" with <code>(..)</code>; if <code>f</code> did not evaluate to a function then an error is thrown. This is why callbacks, which are just functions, can be named by variables and passed as parameters.</p> <p>Also, check the assumption/assertion about the prototype:</p> <pre><code>var abc = function def() {} abc.prototype === abc // -&gt; false abc.name // -&gt; "def" </code></pre> <hr> <p><sup>1</sup> See Bergi's answer.</p> <p><sup>2</sup> How to easily tell which is which?</p> <p>The grammar rules <em>only</em> allow <code>function ..</code> to be parsed as a Function Declaration when it is a <a href="http://es5.github.com/#x14" rel="nofollow">SourceElement</a> although most engines will still [incorrectly] parse a Function Declaration as a <a href="http://es5.github.com/#x12" rel="nofollow">Statement</a>. SourceElement productions <em>only</em> occur at the top-level "block" of a Program or the top-level "block" of a Function.</p> <p>In any case, whenever there is a <code>function ..</code> that appears in a place that <em>requires</em> an <a href="http://es5.github.com/#x11" rel="nofollow">Expression</a>, it <em>will</em> be parsed as a Function Expression. Examples that are <em>all</em> parsed as Function Expressions:</p> <pre><code>// Can only assign values: Statements do not have values! var f = function () {} var g = function gName () {} // see Bergi's answer // Can only pass values: Statements do not have values! doIt(function () {}) // callback / "anonymous function" // IIFE: Immediately-Invoked Function Expression ;(function () {})() // standard IIFE ;(function () {} ()) // alternative standard IIFE ;+function () {} () // curious way to write an IIFE // basically you can use any unary operator to turn it into an expression not only // + but also - ! ~ and so on which will modify the return value accordingly </code></pre> <p>The point is that in <em>every</em> single one of the above cases, <code>function ..</code> appears at a grammar location <em>requiring</em> an Expression and is thus parsed as a Function Expression. (The semicolons at the start of the lines above avoid "ambiguity" with ASI, which is required when writing in a semicolon-free style like I prefer.)</p> <p>However. <code>;function () {} ()</code> and <code>;function f () {} ()</code> are both invalid syntax - why? ;-)</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. 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