July 2000 Draft
JavaScript 2.0
Core Language
Language Declarations
previousupnext

Saturday, April 29, 2000

Language declarations allow a script writer to select the language to use for a script or a particular section of a script. A language denotes either a major language such as JavaScript 2.0 or a variation such as strict mode.

Developers often find it desirable to be able to write a single script that takes advantage of the latest features in a host environment such as a browser while at the same time working in older host environments that do not support these features. JavaScript 2.0's language declarations enable one to easily write such scripts. One may still need to use techniques such as the LANGUAGE HTML attribute to support pre-JavaScript 2.0 environments, but at least the number of such environments that will need to be special-cased will not increase in the future.

Language declarations are a dual of versioning: language declarations let a script run under a variety of historical hosts, while versioning lets a host run a variety of historical scripts.

Syntax

LanguageDeclaration  language [no line break] LanguageIds LanguageAlternatives
LanguageAlternatives 
   «empty»
|  |
|  | LanguageIds LanguageAlternatives
LanguageIds 
   LanguageId
|  LanguageId [no line break] LanguageIds
LanguageId 
   Identifier
|  Number

A language declaration uses the syntax above. The keyword language is followed by one or more language alternatives separated by vertical bars. Each language alternative consists of one or more identifiers or numbers (language identifiers), except that, if there is more than one language alternative, the last one may be empty. The semicolon at the end of the LanguageDeclaration cannot be inserted by line-break semicolon insertion.

When a JavaScript environment is lexing and parsing a JavaScript program and it encounters a language declaration, it checks whether any of the language alternatives can be satisfied. If at least one can, the environment picks the first language alternative that can be satisfied and processes the rest of the containing block (until the closing } or until the end of the program if at the top level) using that language. A subsequent language declaration in the same block can further change the language.

If no language alternatives can be satisfied, then the JavaScript environment skips to the end of the containing block (until the closing matching } or until the end of the program if at the top level). Further language declarations in the same block are ignored. No error occurs unless the failing language declaration is executed as a statement, in which case it throws a syntax error. [See rationale for a discussion of some of the issues here.]

The following language identifiers are currently defined:

Language Identifier   Language
1.0 JavaScript 1.0
1.1 JavaScript 1.1
1.2 JavaScript 1.2
1.3 JavaScript 1.3
1.4 JavaScript 1.4
1.5 JavaScript 1.5 (ECMAScript Edition 3)
2.0 JavaScript 2.0
strict Strict mode
traditional Traditional mode (default)

It is meaningless to combine two or more numeric language identifiers in the same alternative:

language 1.0 2.0;

will always fail. On the other hand, it is meaningful and useful to separate them with vertical bars. For example, one can indicate that one prefers JavaScript 2.1 but is willing to accept JavaScript 2.0 if 2.1 is not available:

language 2.1 | 2.0;

An empty alternative will always succeed. One can use it to indicate a preference for strict mode but willingness to work without it:

language strict |;

Language declarations are always lexically scoped and never extend past the end of the enclosing block.

This document specifies the 2.0 language and its strict and traditional modes. The consequences of mixing in other languages are implementation-defined, but implementations are encouraged to do something reasonable.

Strict Mode

Many parts of JavaScript 2.0 are relaxed or unduly convoluted due to compatibility requirements with JavaScript 1.5. Strict mode sacrifices some of this compatibility for simplicity and additional error checking. Strict mode is intended to be used in newly written JavaScript 2.0 programs, although existing JavaScript 1.5 programs may be retrofitted.

The opposite of strict mode is traditional mode, which is the default. A program can readily mix strict and traditional portions.

Strict mode has the following effects:

See also rationale.


Waldemar Horwat
Last modified Saturday, April 29, 2000
previousupnext