Squish 6.6 ships with several extensions to the JavaScript support. The main ones are described below.
Let Declarations¶
Early JavaScript versions offered the var
keyword for variable declarations:
The sometimes non-intuitive side-effect of var
declarations is their
scoping: they are accessible within the entire function's scope. Or
globally when found outside of a function. And - even more surprising -
access to the variable is possible before the declaration
statement. Even though the value will be undefined
in that case.
The let
keyword does away with those potential problems:
- The variable's life time is restricted to the current
{
...}
block scope. - Access before the declaration statement is not allow
To further avoid variable declaration problems the "use strict"
directive described below is worthwhile using. It will catch cases of
accidental variable usage without a var
or let
declaration.
Const Declarations¶
The const
keyword was available before but had a behavior that
pre-dates the standardization in ECMAScript. As of Squish 6.6.0 const
will have
a) block scope like let
and
b) lead to an error on attempts to change its value
A constant declaration needs to include an initializer value,
i.e. const c;
by itself is not a valid statement.
Arrow Functions¶
A classic function declaration
can be expressed in a short-hand form using the =>
operator:
This notation is particularly convenient for concise expression of callback functions:
Template Strings¶
Tired of assembling strings using the +
operator? A new type of
string literal marked by backticks provides more flexibility than the
classic strings surrounded by single or double quotes.
Instead, of assembling a constant like
multi-line strings can be expressed as a single literal for example:
And instead of mixing constant and variable parts like this
the ${...}
placeholder can embed expressions that will be evaluated
on-the-fly:
Classes¶
Object-oriented programming with JavaScript is based on the concept of
function and prototype objects. A concise declaration of object
prototypes, functions and inheritance is now available via the class
keyword.
Note that the script interpreter has to see the declaration of a class prior to its first usage. That's different to functions that can be referenced even if the declaration follows later in the script.
Constructors¶
A class declaration can contain a special constructor
function. It
will be invoked upon creation of an object via the new
operator. That way an object's initial state can be set safely and
conveniently.
Prototype Methods¶
Function properties of class objects (methods) can be defined with a
shorthand notation that does not include the typical function
keyword:
Static Methods¶
Methods can be declared for the class itself via the static
keyword. They do not require instances of the class and not operate on
them.
Typically, class functions contain utility or factory functions.
Extensions¶
Once a class is declared it can act as a base for other classes that will inherit all its properties.
Super¶
The constructor
function of a complex class may want to invoke the
constructor of the base class. This is possible through the super()
function.
Similarly, the super
property references methods of the extended class:
Class Expressions¶
Classes can also be declared as part of an expression and thus treated like values:
Strict Mode¶
A strict parsing mode will be enabled if the magic string "use strict"
or 'use strict'
is found at the top of a script file or
function. The strict mode helps to detect common programming errors
like access to undeclared variables.
Example:
Currently, the following issues will be detected:
- Access to undeclared variables
- Attempts to
delete
variables or constants - Usage of
with()
statement - Usage of legacy octal literals (e.g. 0123)
Both module code as well as class functions will always be interpreted in strict mode.
The magic "use strict"
string merely needs to be the first statement
within a file or function by the way. Both whitespace and comments can
come first.
Note about Script-based Object Maps¶
Since JavaScript modules will be evaluated in strict mode script-based object maps may need fixing in case they rely on behavior which is not permitted in strict mode.
Exponentiation Operator¶
A short form of the Math.pow(base, exp)
exponentiation function is
available via the new **
operator. It can also be part of an
assignment.
Parameter-less Catch Clause¶
The classic way of catching a script exception in a try
/catch
statement includes the identification of the thrown exception - e
in
the example below:
Miscellaneous¶
New style for octal literals (such as
0o123
and0O123
)The
Intl.DateTimeFormat().resolvedOptions()
function that will the localelocale
andtimeZone
configurationThe
Object.is(value1, value2)
comparison functionThe
Object.setPrototype(O, proto)
functionThe
Console
object was renamed toconsole
to be in line with what web browsers use. The object provides theassert()
function.Fixed the test method on JavaScript RegExp objects such that calls update the value of the lastIndex field if the global flag is specified.
The
Object.getPrototypeOf(O)
function now works with non-object values according to ECMAScript 2015.Syntax-checking of escape sequences in string literals is now more accurate.
Line continuations in JavaScript string literals now work as expected.