Environment Notes

The Javascript engine is ECMAScript 5/5.1 compliant. You can also use many ES2015 features and some decorators. The following babel plugins are enabled:

babel-plugin-check-es2015-constants
babel-plugin-transform-decorators-legacy
babel-plugin-transform-class-properties
babel-plugin-transform-regenerator
babel-plugin-transform-es2015-arrow-functions
babel-plugin-transform-es2015-block-scoped-functions
babel-plugin-transform-es2015-block-scoping
babel-plugin-transform-es2015-classes
babel-plugin-transform-es2015-computed-properties
babel-plugin-transform-es2015-duplicate-keys
babel-plugin-transform-es2015-function-name
babel-plugin-transform-es2015-literals
babel-plugin-transform-es2015-modules-commonjs
babel-plugin-transform-es2015-object-super
babel-plugin-transform-es2015-parameters
babel-plugin-transform-es2015-shorthand-properties
babel-plugin-transform-es2015-spread
babel-plugin-transform-es2015-template-literals
babel-plugin-transform-es2015-destructuring
babel-plugin-transform-es2015-for-of
babel-plugin-transform-es2015-typeof-symbol
babel-plugin-transform-object-rest-spread

The following decorators are built-in and can be imported using the decorators-{decoratorName} convention: autobind, decorate, deprecate, enumerable, extendDescriptor, lazyInitialize, nonconfigurable, nonenumerable, override, readonly.

import autobind from 'decorators-autobind';
import readonly from 'decorators-readonly';

class Eye {   
    
    @readonly
    pie = 'pie'; // initializable properties support.

    @autobind
    apple() {
        return this.pie;
    }
    
    orange = () => this.pie; // bound functions support.
}

const eye = new Eye();

eye.apple.call(null); // 'pie' returned even though bound to null.
eye.orange.call(null); // 'pie' returned even though bound to null.
eye.pie = 'not allowed'; // throws

Module loading is available via CommonJS; Standard and custom modules are loaded using require and defined using module.exports. Imports and exports are also supported.

For example, a module exported as c_utils, defined as:

import _  from 'underscore';

export default {
  
  dateToAge: function(birthDate) {
    if (!_.isDate(birthDate)) return 0;
    
    const today = new Date(),
          m = today.getMonth() - birthDate.getMonth();
    
    let age = today.getFullYear() - birthDate.getFullYear();
    
    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
      age--;
    }
    return age;
  }
};

might be loaded and used in a route as follows:

import utils from 'c_utils';

const me = org.objects.Accounts
  .find({
    _id: script.principal._id
  })
  .paths('dob')
  .next();

return utils.dateToAge(new Date(me.dob));

All scripts run in strict mode. For triggers, jobs and routes the entry point to the script is called main, which can be seen in script stack traces. In order to get meaningful stack traces, it's good practice to name all functions.

Stack traces are available for debugging via the Error object's trace property or the getCurrentStackTrace global function. A stack trace is also provided with calls to the logger or console modules.

The console, consts, org and script auto-globals are always guaranteed to be available, though console printing only works on the development servers.