Skip to content

JsExtensions

Chris Lowth edited this page Sep 21, 2022 · 5 revisions

TbScript language Extensions

TbScript and TbUtil are based on the open-source Otto JavaScript interpreter which implements the ECMA Script 5 standard.

We have made some extensions to the interpreter for TbScript to adopt partial support for a few nice ES6 features. These are listed here.

Template strings

TbScript has limited support for ES6 template strings (this is implemented as a patch to the "Otto" interpreter code).

This feature is used when strings are specified between a pair of back-ticks. In this case, the string can span multiple lines and is passed to the function "interpolate" for processing at the point of declaration. This makes the following ...

var a = `Hello ${who},
Nice to see you`;

Equivalent to ...

var a = interpolate("Hello ${who}\n" +
"Nice to see you");

Unlike ES6, you cannot prepend the name of a function to the string to specify custom processing. Instead, you can declare an "interpolate" function of your own in the calling context.

The default "interpolate" function works like the ES6 template parser.

See "interpolate" function (in JS-ADDINS) for more details.

Fat arrow functions.

Fat-Arrow functions are "syntactic sugar" that are typically used to declare callback functions in a more visually appealing way. For example, the code...

myList.forEach(entity => { println(entity.name); });

Is equivalent to...

myList.forEach(function(entity) { println(entity.name); });

Our implementation of this feature works by performing source code parsing when scripts are loaded into tbscript (rather than by patching the otto interpreter). The following differences exist between tbscript's implementation and the full ES6 versions...

  • The code MUST be enclosed in braces or brackets following the arrow. Brackets are used to represent a single expression and a "return" is implied in this case. Braces are used to contain more complex logic and if the function returns a value, the code must include a "return" statement.
  • The function has the same scope conventions as a regular anonymous function. In particular there is no special handling of the "this" variable (so it's use should be avoided).

Example: given the list of numbers, the three following lines are equivalent.

var numbers = [1,2,3,4,5,6];

var oddNumbers = numbers.filter(n => ( n % 2 ));
var oddNumbers = numbers.filter(n => { return n % 2; });
var oddNumbers = numbers.filter(function(n) { return n % 2; });

Optional Chaining

TbScript provides partial support for the "?." optional chaining operator. This is typically used when traversing the fields of a hierarchical object and returns null (rather than throwing an exception) when the left hand side is null or undefined.

For example, the following code prints "undefined" and then fails with the exception "Cannot access member 'two' of undefined" ...

var x = { };
println(x.one);
println(x.one.two);

Where as this code prints "undefined" twice and does not throw an exception ...

var x = { };
println(x?.one);
println(x?.one?.two);

Unlike true ES6, this does not work when used to call functions. The following code fails with the error "undefined is not a function" (where as true ES6 would print "undefined").

var x = { };
println(x?.one?.two());

Custom functions and global variables.

TbScript supports an extensive set of functions, global variables, object types and additional object methods that go beyond those supported by the Otto interpreter. These are documented in JS-ADDINS. They include (but are not limited to)..

  • Functions for printing and formatting text.
  • Functions to support the creation of tables, spreadsheets etc.
  • Functions for loading data from files and web URLs.
  • Functions to support JavaScript modules and GOLang plugins.
  • Additional string and array object helper methods.
  • Additional Object types. For example: for accessing the Turbonomic API.
  • Functions for simple multi-threaded code execution.
  • Functions for implementing simple web servers.
  • Functions for accessing OS features (eg: DNS resolution, the environment, the file system, shell commands - etc).

And lots more.

Clone this wiki locally