You probably have stumbled over strange first lines in scripts that I have posted here. If you are a C developer, you are most likely are familiar with #pragma directives, a way to tell the C compiler what to do or how to behave inside the source code, not outside at command line.
Java script is an interpreted language, however, there is also the need to tell the interpreter how to behave in certain situations.Similar to #pragma we have introduced a few directives over time to give the script to affect its usage in the execution environment. The directives are enclosed by two $ signs, for example $DO_SOMETHING$.
The $$ directives are processed while the script is being parsed. They have to be placed in the first line of the script, and since they are not following the JS syntax, they have to be placed inside a comment:
An example:
// $EXPERIMENTAL$ $STRICT_MODE$ $WARNING_AS_ERROR$
alert("This script must strictly follow ECMAScript 5 restricted variant of JavaScript");
There are 3 directives in this script:
- $STRICT_MODE$ forces the interpreter to warn when the script is not following the rules
- $WARNING_AS_ERROR$ will turn these warnings into errors, so the interpreter will stop on simple syntax errors, variable re-declarations, missing ; and so forth
- $EXPERIMENTAL$ enables further features that are not part of the public API for example the mighty "bind" method you may have seen
This is by the way what Mozilla says about "sloppy mode":
ECMAScript 5 and later let scripts opt in to a new strict mode, which alters the semantics of JavaScript in several ways to improve its resiliency and which make it easier to understand what's going on when there are problems.
The normal, non-strict mode of JavaScript is sometimes referred to as sloppy mode. This isn't an official designation, but you are likely to come across it if you spend time doing serious JavaScript code.
The following further directives exist:
- $DEBUG$ opens the debugger, see separate article on debugging
- $ENHANCED_CONTAINMENT_ACCESS$ allows scripts to rearrange and move elements, use with care (!) since (4.2.0)
- $NO_OPTIMIZATION$ scripts are not optimized, e.g. not compiled to java byte code, use it if you face memory limits (19.1.0)
Note that these directives have to be used with extreme care.
Happy Scripting
Jan