tags:

views:

74

answers:

1

The production VariableStatement :

var VariableDeclarationList ; 

is evaluated as follows:

  1. Evaluate VariableDeclarationList.
  2. Return (normal, empty, empty).

I want to know about meaning of normal and empty.

+6  A: 

ECMAScript describes an internal type to explain the behavior of statements, it's called The Completion Specification Type.

Values of the Completion type are triples of the form of (type, value, target), where:

  • type can be normal, break, continue, return, or throw.
  • value can be any language value or empty.
  • target can be any Identifier or empty.

In the case of the VariableStatement, the returned completion has no observable effect, it is a normal completion, since the flow of control is not modified.

Other statements that return a normal completion, are e.g. an empty Block statement, the Empty Statement, the ExpressionStatement, the debugger statement (when no debugger attached), etc...

A FunctionDeclaration (which is not a statement, but a SourceElement) also returns a (normal, empty, empty) completion, thats why for example:

eval("function f(){}"); // returns undefined

The eval function after executing the code, examines the completion result, and if the type is normal and the value is empty, it explicitly produces undefined (see step 7 of eval), whereas:

eval("(function f(){})"); // returns a function object

There the parentheses build a PrimaryExpression, which is part of an ExpressionStatement, and this statement returns the completion (normal, GetValue(exprRef), empty), where expRef will be the value of the FunctionExpression.

If the completion type if other than normal is known also as an "abrupt completion".

For example:

function foo() {
  return 5;
}
foo();

The return statement inside foo will produce a completion that looks like (return, 4, empty).

The target value in the triplet is only used by break and continue, to reference an identifier of a LabelledStatement, e.g.:

foo: while(true) {
  while(true) {
    break foo;
  }
}

The completion result of the above break statement would be (break, empty, foo), since the flow of control is transfered from within the second while to outside, at the level of the foo label.

You can see more details about how this internal type is used, on all other statements that perform nonlocal transfers of control as break, continue, return and throw.

CMS
Thank you for your help
okjungsoo
@okjungsoo: You're welcome, and welcome to StackOverflow, note that if an answer satisfies your question, you can (and should) mark your questions [as answered](http://meta.stackoverflow.com/questions/5234/how-does-accepting-an-answer-work), in that way you start building a [good accept rate](http://meta.stackoverflow.com/questions/16721/what-is-an-accept-rate-and-how-does-it-work). Cheers.
CMS