Compiler Checking Mode
The Compiler checking mode controls how aggressively the compiler warns about potential program errors.
Ejscript supports two checking modes:
- 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.
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 script1.es ejs --strict script2.es
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 */