Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VariableStatement semantics in ECMAScript

Tags:

javascript

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.

like image 564
okjungsoo Avatar asked Oct 18 '10 07:10

okjungsoo


People also ask

What does => mean in ES6?

It's a new feature that introduced in ES6 and is called arrow function. The left part denotes the input of a function and the right part the output of that function.

What is the difference between ECMAScript and JavaScript?

JavaScript is a general-purpose scripting language that conforms to the ECMAScript specification. The ECMAScript specification is a blueprint for creating a scripting language. JavaScript is an implementation of that blueprint. On the whole, JavaScript implements the ECMAScript specification as described in ECMA-262.

What is ECMAScript example?

ECMAScript (/ˈɛkməskrɪpt/; ES) is a JavaScript standard intended to ensure the interoperability of web pages across different browsers. It is standardized by Ecma International in the document ECMA-262.


1 Answers

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.

like image 153
Christian C. Salvadó Avatar answered Oct 19 '22 14:10

Christian C. Salvadó