Language Modes

Ejscript is fully compliant with the JavaScript (ECMA-262 3.X) standards, but it also offers a set of enhancements and fixes designed to improve the language and correct some longstanding JavaScript design issues. Use of these enhancements and fixes is optional and controlled via opt-in configuration and script pragma directives. Language modes can be selected on a source file-by-file basis. So you can mix and match ECMA-262 pure JavaScript with enhanced Ejscript code if you wish.

Compiler Checking Mode

The Compiler checking mode controls how aggressively the compiler warns about potential program errors.

Ejscript supports two checking modes:

  1. standard — Traditional JavaScript compilation with little compile time checking
  2. strict — Uses strict static type checking at compile time

Strict mode is useful for libraries where detecting errors before run-time is highly desirable. Strict mode also assists the compiler to generate faster code as the compiler knows the location and type of all variables ahead of time. However, it does restrict your coding style by requiring that all variables be declared and typed — this should be used on a case-by-case basis.

For users of a module, it does not matter whether the module was created using strict or standard checking. The checking mode only impacts the module source code itself and not the users of the module.

The compiler checking mode can be controlled by a use standard or use strict pragma directive in your scripts. Like the Language Compliance pragma, this pragma takes effect on a file by file basis.

Standard Mode

Standard mode is the traditional JavaScript compilation behavior. Variables do not have to be declared ahead of time and no warnings are issued for incompatible type usage. Issues are resolved at run-time, by dynamic casting or by issuing exceptions.

Strict Mode

Strict mode applies compile time static checking and type conversions to try to handle and catch errors before run-time. In strict mode, variables must be declared before they are used. If the script code is not using ecma language compliance, variables must also be typed. Functions must define a return type and must type their parameters.

Setting the Compiler Checking Mode

The compiler mode control is set to a default value by command line switches to the ejs shell or ec compiler. This can be overridden by an Ejscript pragma directive in your script files on a file-by-file basis.

Command Line Compiler Checking Switch

To set the compiler checking mode on the ejs or ec commands, use the --strict or --standard switch. The default is --standard.

ejs --standard
ejs --strict

Use Strict and Use Standard Pragmas

To set the compiler checking mode for a source file, utilize the use strict or use standard pragma directives. These pragmas will override the default value for the current source file.

/* In source file 1 */
use strict
/* In source file 2 */
use standard
/* In source file 3 */
/* No use pragma - just accept the current default */

© Embedthis Software. All rights reserved.