6.8 Functions


ECMA Script functions are specified in terms of FunctionDeclaration and FunctionExpression :

FunctionDeclaration : function Identifier ( FormalParameterList opt ) { FunctionBody }

FunctionExpression : function Identifier opt ( FormalParameterList opt ) { FunctionBody }

FormalParameterList : FormalParameterList opt , Identifier

These expressions for FunctionDeclaration and FunctionExpression are processed for function declarations and evaluated in two steps as follows : First, the script engine creates a new Function object with parameters specified by FormalParameterList , and body specified by FunctionBody ; the scope chain of the running execution context is used for the current execution scope. Next, for FunctionDeclaration , the script engine creates a property of the current variable object with name Identifier and value resulting from the first step above. For FunctionExpression the script engine creates a similar property but, in case that Identifier is not present (i.e., anonymous function), assigns this property an internal name and renders it inaccessible to the script program.

The Identifier 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 Identifier in a FunctionExpression cannot be referenced from and does not affect the scope enclosing the FunctionExpression .

Two functions are considered to be the equal in the following cases:

  • Both functions obtained their FunctionBody from the same location in the source text of the same ECMA Script program. This source text consists of global code and possibly additional contained function codes.

  • Both functions obtained their FunctionBody from the same location in the source text of the same call to eval() . This source text consists of eval() code and possibly additional contained function codes.

Functions obtained from distinct calls to the Function constructor or calls to eval() are never considered the equal, even if those two calls to eval() were passed the same argument.



ITV Handbook. Technologies and Standards
ITV Handbook: Technologies and Standards
ISBN: 0131003127
EAN: 2147483647
Year: 2003
Pages: 170

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net