Can I use in Google Apps Scripts a defined Class in a library with ES6 (V8)?

As written in the official documentation,

Only the following properties in the script are available to library users:

  • enumerable global properties
    • function declarations,
    • variables created outside a function with var, and
    • properties explicitly set on the global object.

This would mean every property in the global this object are available to library users.

Before ES6, All declarations outside a function (and function declaration themselves) were properties of this global object. After ES6, There are two kinds of global records:

  • Object record- Same as ES5.

    • Function declarations
    • Function generators
    • Variable assignments
  • Declarative record – New

    • Everything else – let, const, class

Those in the declarative record are not accessible from the global “object”, though they are globals themselves. Thus, the class declaration in the library is not accessible to library users. You could simply add a variable assignment to the class to add a property to the global object(outside any function):

var Update = class Update{/*your code here*/}

References:

Leave a Comment