Prototype.js, ServiceNow, and Objects

Disclaimer: This article assumes some entry level computer science concepts, and it’s a bit dry!

A well known programming concept is Classes, and their instance, Objects, which is a common staple of Object Oriented Programming (OOP) languages. Older versions of javascript (ES5 and below) which is what ServiceNow uses don’t have support for Classes. In an attempt to solve this problem, to add support for the concept of classes, ServiceNow used Prototype.js. A common place prototype is used for all the classes we define in ServiceNow's Script Includes.

Let’s take a look at an example Prototype Class that also could be a script include.

var Pirate = Class.create();
Pirate.prototype = {
  initialize: function(name) {
    this.name = name;
  },
  say: function(message) {
    return this.name + ': yarr ' + message;
  }
  type: 'Pirate'
};

The key parts are defining a class, it’s prototype, initialize method (aka the constructor, which can be optional), functions (aka methods), and the type variable which ServiceNow uses to reference script includes.

Inheritance & Polymorphism

Prototype also has support for common OOP concepts like Inheritance, with super and sub classes. A well known example for this is the GlideAjax super class. Along with Inheritance support, there is also support for method overriding and polymorphism. Since methods can be overridden, you don’t want to override like initialize function when extending the AbstractAjaxProcessor, otherwise it would break.

var myAjax = Class.create();
myAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
      getMessage: function () {
        return "success"
      }
});

Encapsulation & Private Elements

Another capability is encapsulation, and support for private functions and data variables. You can set a function or data element as private by using the “_” underscore prefix in front of the name. The data or function can only be called and accessed from within the class itself by using the this._privateVar syntax. All other methods and classes are by default considered public, and there is no concept of protected variables.

This Object

I’ve referenced it in the initial example, but Prototype provides a very helpful “this” object, which allows you to store data variable and call other functions within the current function. This also is incredibly helpful because with the type variable on all script includes, it’s very easy to log and identify where something is coming from. This is very similar to the “current” object available within business rules and workflows, but provides you with real accurate information regarding the script location.

Closing Thoughts

I’m not entirely sure which version of Prototype ServiceNow is running, but it seems to be older than 1.6.0 since there isn’t support for superclass or subclass properties. I can only assume a version was selected around 2004 when ServiceNow was initially developed and released. Script includes are one of the greatest capabilities that ServiceNow has to offer for re-usable server side code snippets, and prototype.js enables it to be that much more powerful.