>From the ES5 spec section 12 statement semantics:

NOTE    Several widely used implementations of ECMAScript are known to support 
the use of FunctionDeclaration as a Statement. However there are significant 
and irreconcilable variations among the implementations in the semantics 
applied to such FunctionDeclarations. Because of these irreconcilable 
differences , the use of a FunctionDeclaration as a Statement results in code 
that is not reliably portable among implementations. It is recommended that 
ECMAScript implementations either disallow this usage of FunctionDeclaration or 
issue a warning when such a usage is encountered. Future editions of ECMAScript 
may define alternative portable means for declaring functions in a Statement 
context.

Also see section on functions in 
http://blogs.msdn.com/b/ie/archive/2010/08/25/chakra-interoperability-means-more-than-just-standards.aspx
 

There seem to be more browsers that treat function declarations in blocks as 
straightforward extensions of the function hoisting semantics (unconditionally) 
but Firefox does something different.

As I say in the above blog post, you have to have them in a browser but 
anything other than trivial usage is unlike to be interoperable.

Allen
 

> -----Original Message-----
> From: es-discuss-boun...@mozilla.org [mailto:es-discuss-
> boun...@mozilla.org] On Behalf Of Michael Day
> Sent: Thursday, November 11, 2010 3:46 PM
> To: ES-Discuss
> Subject: Function declarations as statements
> 
> Hi,
> 
> In ECMAScript, function declarations are SourceElements, but not Statements.
> This means that they can only occur at the top level, and may not be nested
> inside a block.
> 
> However, browsers support function declarations as statements, and many
> scripts on the web seem to make use of this ability, especially after they 
> have
> been minified.
> 
> Is there a spec anywhere for how functions as statements should behave?
> 
> Can they be rewritten to var + function expression, so that this:
> 
> {
>      function f() { return 17 }
> }
> 
> becomes this:
> 
> {
>      var f = function() { return 17 }
> }
> 
> When implementing non-standard features, it would be nice to do so in a
> standard manner :)
> 
> Best regards,
> 
> Michael
> 
> --
> Print XML with Prince!
> http://www.princexml.com
> _______________________________________________
> es-discuss mailing list
> es-discuss@mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to