Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

May function declarations appear inside statements in JavaScript?

Tags:

javascript

Please consider the official ECMAScript specification as the source for your answer, and not a document published by a specific browser vendor. (I am aware of Mozilla extending its JavaScript implementation with "function statements".)

So, according to the ECMAScript spec, ergo, the syntactic productions defined in it, is this valid?

if (foo) {
    function x() { return; }
}

Update: My question can also be phrased like so: Can the Statement production contain the FunctionDeclaration production?

Conclusion: The answer is NO.

like image 840
Šime Vidas Avatar asked Nov 01 '10 17:11

Šime Vidas


People also ask

Can we write function inside if statement in JavaScript?

The code in the question will result in an undefined function error because the function a is only declared within the scope of if statements and therefore doesn't exist in the global scope. If you need to conditionally define a function, then you should use function expressions.

How are functions declared in JavaScript?

A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses ().

What goes inside a function JavaScript?

A function definition (also called a function declaration, or function statement) consists of the function keyword, followed by: The name of the function. A list of parameters to the function, enclosed in parentheses and separated by commas.

What are declaration statements in JavaScript?

JavaScript applications consist of statements with an appropriate syntax. A single statement may span multiple lines. Multiple statements may occur on a single line if each statement is separated by a semicolon. This isn't a keyword, but a group of keywords.


1 Answers

I do not agree with the other answers that say it is valid.

According to the ECMA-262 5th Edition specification, Blocks can only contain Statements (Section 12.1):

Block :
   { StatementList opt }

StatementList :
   Statement
   StatementList  Statement

However the spec does not define a function statement, but only a FunctionDeclaration and a FunctionExpression. The spec goes further to make a note on this in Section 12:

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 difference, 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.

For further reading, you may also be interested in checking out the comp.lang.javascript FAQ Section 4.2:

4.2 What is a function statement?

The term function statement has been widely and wrongly used to describe a FunctionDeclaration. This is misleading because in ECMAScript, a FunctionDeclaration is not a Statement; there are places in a program where a Statement is permitted but a FunctionDeclaration is not. To add to this confusion, some implementations, notably Mozillas', provide a syntax extension called function statement. This is allowed under section 16 of ECMA-262, Editions 3 and 5.

Example of nonstandard function statement:

// Nonstandard syntax, found in GMail source code. DO NOT USE.
try {
  // FunctionDeclaration not allowed in Block.
  function Fze(b,a){return b.unselectable=a}
  /*...*/
} catch(e) { _DumpException(e) }

Code that uses function statement has three known interpretations. Some implementations process Fze as a Statement, in order. Others, including JScript, evaluate Fze upon entering the execution context that it appears in. Yet others, notably DMDScript and default configuration of BESEN, throw a SyntaxError.

For consistent behavior across implementations, do not use function statement; use either FunctionExpression or FunctionDeclaration instead.

Example of FunctionExpression (valid):

var Fze;
try {
  Fze = function(b,a){return b.unselectable=a};
  /*...*/
} catch(e) { _DumpException(e) }

Example of FunctionDeclaration (valid):

// Program code
function aa(b,a){return b.unselectable=a}
like image 191
Daniel Vassallo Avatar answered Oct 21 '22 14:10

Daniel Vassallo