JavaScript 2.0
Core Language
Pragmas
previousupnext

Tuesday, January 28, 2003

Pragmas allow a script writer to select program modes and options. Pragmas are lexically scoped and their effect does not extend past the end of the enclosing block, directive group, or substatement group.

Pragma  use PragmaItems
PragmaItems 
   PragmaItem
|  PragmaItems , PragmaItem
PragmaItem 
   PragmaExpr
|  PragmaExpr ?
PragmaExpr 
   Identifier
|  Identifier ( PragmaArgument )
PragmaArgument 
   true
|  false
|  Number
|  - Number
|  - NegatedMinLong
|  String

The keyword use is followed by one or more PragmaItems, each of which consists of an identifier, an optional argument, and an optional ?.

The pragma identifiers below are currently defined. Implementations may define additional identifiers that have meaning as pragmas.

Identifier   Meaning
ecmascript(n) Error if version n of ECMAScript is not supported; otherwise recommends but does not require that the implementation only support ECMAScript version n features.
javascript(n) Error if version n of JavaScript is not supported; otherwise recommends but does not require that the implementation only support JavaScript version n features.
strict
strict(true)
Strict mode
strict(false) Non-strict mode (default)

The pragma takes effect starting with the directive after the pragma and continues either until the end of the enclosing block or statement group or until overridden by another pragma in the same block or statement group, whichever comes first. If a pragma references the same identifier several times, the last reference takes precedence. The semicolon insertion rule changes implied by the strict pragma apply to the semicolon, if any, ending the use directive that contains that pragma.

If an implementation does not recognize a pragma identifier, then if the PragmaItem ends with a ? then that PragmaItem is ignored; if the PragmaItem does not end with a ? then an error occurs.

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 nonstrict mode, which is the default. A program can readily mix strict and nonstrict portions.

Strict mode has the following effects:

An implementation does not have to implement strict mode; however, implementations are encouraged to do so.

See also the rationale.


Waldemar Horwat
Last modified Tuesday, January 28, 2003
previousupnext