15.2 Function Definitions

Syntax

FunctionDeclaration[Yield, Await, Default] : function BindingIdentifier[?Yield, ?Await] ( FormalParameters[~Yield, ~Await] ) { FunctionBody[~Yield, ~Await] } [+Default] function ( FormalParameters[~Yield, ~Await] ) { FunctionBody[~Yield, ~Await] } FunctionExpression : function BindingIdentifier[~Yield, ~Await]opt ( FormalParameters[~Yield, ~Await] ) { FunctionBody[~Yield, ~Await] } FunctionBody[Yield, Await] : FunctionStatementList[?Yield, ?Await] FunctionStatementList[Yield, Await] : StatementList[?Yield, ?Await, +Return]opt

15.2.1 Static Semantics: Early Errors

FunctionDeclaration : function BindingIdentifier ( FormalParameters ) { FunctionBody } function ( FormalParameters ) { FunctionBody } FunctionExpression : function BindingIdentifieropt ( FormalParameters ) { FunctionBody } Note

The LexicallyDeclaredNames of a FunctionBody does not include identifiers bound using var or function declarations.

FunctionBody : FunctionStatementList

15.2.2 Static Semantics: FunctionBodyContainsUseStrict

FunctionBody : FunctionStatementList
  1. If the Directive Prologue of FunctionBody contains a Use Strict Directive, return true; otherwise, return false.

15.2.3 Runtime Semantics: EvaluateFunctionBody

With parameters functionObject and argumentsList (a List).

FunctionBody : FunctionStatementList
  1. Perform ? FunctionDeclarationInstantiation(functionObject, argumentsList).
  2. Return the result of evaluating FunctionStatementList.

15.2.4 Runtime Semantics: InstantiateOrdinaryFunctionObject

With parameter scope.

FunctionDeclaration : function BindingIdentifier ( FormalParameters ) { FunctionBody }
  1. Let name be StringValue of BindingIdentifier.
  2. Let sourceText be the source text matched by FunctionDeclaration.
  3. Let F be OrdinaryFunctionCreate(%Function.prototype%, sourceText, FormalParameters, FunctionBody, non-lexical-this, scope).
  4. Perform SetFunctionName(F, name).
  5. Perform MakeConstructor(F).
  6. Return F.
FunctionDeclaration : function ( FormalParameters ) { FunctionBody }
  1. Let sourceText be the source text matched by FunctionDeclaration.
  2. Let F be OrdinaryFunctionCreate(%Function.prototype%, sourceText, FormalParameters, FunctionBody, non-lexical-this, scope).
  3. Perform SetFunctionName(F, "default").
  4. Perform MakeConstructor(F).
  5. Return F.
Note

An anonymous FunctionDeclaration can only occur as part of an export default declaration, and its function code is therefore always strict mode code.

15.2.5 Runtime Semantics: InstantiateOrdinaryFunctionExpression

With optional parameter name.

FunctionExpression : function ( FormalParameters ) { FunctionBody }
  1. If name is not present, set name to "".
  2. Let scope be the LexicalEnvironment of the running execution context.
  3. Let sourceText be the source text matched by FunctionExpression.
  4. Let closure be OrdinaryFunctionCreate(%Function.prototype%, sourceText, FormalParameters, FunctionBody, non-lexical-this, scope).
  5. Perform SetFunctionName(closure, name).
  6. Perform MakeConstructor(closure).
  7. Return closure.
FunctionExpression : function BindingIdentifier ( FormalParameters ) { FunctionBody }
  1. Assert: name is not present.
  2. Set name to StringValue of BindingIdentifier.
  3. Let scope be the running execution context's LexicalEnvironment.
  4. Let funcEnv be NewDeclarativeEnvironment(scope).
  5. Perform funcEnv.CreateImmutableBinding(name, false).
  6. Let sourceText be the source text matched by FunctionExpression.
  7. Let closure be OrdinaryFunctionCreate(%Function.prototype%, sourceText, FormalParameters, FunctionBody, non-lexical-this, funcEnv).
  8. Perform SetFunctionName(closure, name).
  9. Perform MakeConstructor(closure).
  10. Perform funcEnv.InitializeBinding(name, closure).
  11. Return closure.
Note

The BindingIdentifier in a FunctionExpression can be referenced from inside the FunctionExpression's FunctionBody to allow the function to call itself recursively. However, unlike in a FunctionDeclaration, the BindingIdentifier in a FunctionExpression cannot be referenced from and does not affect the scope enclosing the FunctionExpression.

15.2.6 Runtime Semantics: Evaluation

FunctionDeclaration : function BindingIdentifier ( FormalParameters ) { FunctionBody }
  1. Return NormalCompletion(empty).
Note 1

An alternative semantics is provided in B.3.3.

FunctionDeclaration : function ( FormalParameters ) { FunctionBody }
  1. Return NormalCompletion(empty).
FunctionExpression : function BindingIdentifieropt ( FormalParameters ) { FunctionBody }
  1. Return InstantiateOrdinaryFunctionExpression of FunctionExpression.
Note 2

A "prototype" property is automatically created for every function defined using a FunctionDeclaration or FunctionExpression, to allow for the possibility that the function will be used as a constructor.

FunctionStatementList : [empty]
  1. Return NormalCompletion(undefined).