According to the latest v8 runtime update, Google Apps Script now supports the use of classes, whether in libraries or standalone scripts. An example of this implementation can be found in the following documentation:
Classes
Classes offer a way to organize code in a more structured manner with the concept of inheritance. In V8, classes act as a facade for the traditional JavaScript prototype-based inheritance.
To utilize classes, define your class within a script project and deploy it as a Library. Make sure to keep note of the script ID.
class Rectangle {
constructor(width, height) { // class constructor
this.width = width;
this.height = height;
}
logToConsole() { // class method
console.log(`Rectangle(width=${this.width}, height=${this.height})`);
}
}
function newRectangle(width, height) {
return new Rectangle(width, height)
}
Next, in your main application, include the library using the previously noted script ID, create an instance, and invoke the method:
function myFunction() {
const r = RectangleLibrary.newRectangle(10, 20);
r.logToConsole();
}
Sample Output:
https://i.sstatic.net/ea0eX.png
https://i.sstatic.net/2llUp.png