I’m looking at some code from EcmaScript.NET and in particular, I’m looking at the definitions in FunctionNode.cs. They’ve provided a relatively descriptive comment above the definitions, but I’m not sure what would my example below qualify as:
/// <summary>
/// There are three types of functions that can be defined. The first
/// is a function statement. This is a function appearing as a top-level
/// statement (i.e., not nested inside some other statement) in either a
/// script or a function.
///
/// The second is a function expression, which is a function appearing in
/// an expression except for the third type, which is...
///
/// The third type is a function expression where the expression is the
/// top-level expression in an expression statement.
///
/// The three types of functions have different treatment and must be
/// distinquished.
/// </summary>
public const int FUNCTION_STATEMENT = 1;
public const int FUNCTION_EXPRESSION = 2;
public const int FUNCTION_EXPRESSION_STATEMENT = 3;
Here is what I’m roughly seeing:
<script>
(function(){document.write("The name is Bond, ")})(),
(function(){document.write("James Bond.")})()
</script>
Would this qualify as a FUNCTION_STATEMENT, FUNCTION_EXPRESSION, FUNCTION_EXPRESSION_STATEMENT?
Update
I guess my question is about the role of the comma:
// Expression
(function(){ document.write('Expression1<br>'); })();
(function(){ document.write('Expression2<br>'); })();
// Expression
var showAlert=function(){ document.write('Expression3<br>'); };
showAlert();
// Declaration
function doAlert(){ document.write('Declaration<br>'); }
doAlert();
// What about this?
(function(){ document.write('What about'); })(), // <-- Note the comma
(function(){ document.write(' this?<br>'); })();
// And now this?
var a = ((function(){ return 1; })(), // <-- Again, a comma
(function(){ return 2; })());
document.write("And now this? a = " + a);
What are the last two? Expressions or Expression Statements?
Don’t know about EcmaScript.NET, but from my understanding all those functions are function expressions. They are part of a IIFE-invocation, which again is part of a comma-operator expression – in no way “top-level“.
I’d say that the third type are function statements where they are not allowed syntactically, like
Check out Kangax’ famous article about named function expressions, where their behaviour across engines is summed up (e.g. Gecko’s function statements).