- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
This post is the first in a series, intended to be an introduction into writing object-oriented programs in JavaScript. All of the examples will be done in the Service-now.com server-side environment, but the subjects covered are just as applicable to writing client-side (in the browser) JavaScript — the only differences are details about where the code goes and the collection of browser-specific classes and functions that have no equivalent on the Service-now.com instance (server-side).
The cute little fellow at right is Miki, our youngest field spaniel, when he was just ten weeks old. That was back in 2006; he's now all grown up and competing in canine agility trials. He's still cute, and he still likes sticks. He's also (unbeknownst to him) the star of today's post: we're going to make a Dog class, and he's going to be the first object of that class.
Did I just descend into gobbledegook?
In object-oriented (OO) lingo, a class is the template for a kind of object. If I talk about a Dog class, I'm talking about a template for an object that represents dogs. That Dog template can define several things about Dog objects, but the most important thing it can define are the properties of Dog objects. We're going to start by creating a Dog class that is a template for Dog objects that has two value properties: the dog's "call name" (the name you use everyday, as opposed to the formal registered name if the dog is purebred) and the dog's birth date.On a Service-now.com instance there is a very convenient place to define a class: in the Script Includes table (navigate System Definition → Script Includes). Here's what my Dog class looks like in its very own Script Include:
Some things to note:
- This code looks almost exactly like any other function definition. The only real difference is the use of this — a special object reference that always refers to the current object. The line this.birth_date, for example, just sets the value of the birth_date property in the current object. This kind of function is called a constructor because its job is to "construct" an object from its arguments, according to the class template. In JavaScript, the constructor is the class template. In our example here, the template says there are two value properties in Dog objects: birth_date and call_name.
- Constructor functions have one other very special behavior: when invoked with the new keyword, they automatically return this — the newly created object. You can override this behavior by supplying your own return statement (returning some other value), but there is seldom a need to do this. Returning this is exactly what you'd expect — see the test code below.
- The Active field must be checked in order for the Script Include to work.
- The Name field must be exactly the same as the function name in order for the automagical inclusion to work. More on that below.
- The Description field is just for us humans; the system doesn't use it at all. It's your opportunity to tell others just what your class is for.
- By convention, the first character of any class is upper-case (so Dog, not dog). While this is just a convention, and in fact it would all work fine no matter how you spelled it…it's a very worthwhile convention that I strongly suggest you follow. Doing so makes your code far more readable to any fellow JavaScript programmer.
- If you look at some of the built-in JavaScript classes that appear in other Script Includes, you may notice that different syntax is being used to define the classes. Don't worry about that for now; it's just a different (and more convenient) way of doing the same thing, and I'll be explaining how it works in later posts. For now I'm going to show you how JavaScript classes and objects really work; we'll make them more convenient to use later on.
Ok, now we've got our Dog class defined (we'll refine and expand it in later posts). Here's how we can actually use it in a test script:
var dog = new Dog('Miki', new Date(2006,1,11));
var dog_age_ms = new Date().getTime() - dog.birth_date.getTime();
var dog_age_days = dog_age_ms / (1000 * 60 * 60 * 24);
var dog_age_years = Math.floor(dog_age_days / 365.25);
gs.log('Hey ' + dog.call_name + '! You\'re ' + dog_age_years + ' years old today!');
if (dog instanceof Dog)
gs.log('It\'s a dog!');
In the first line, we're creating a new Dog object. Note the use of the new key word: that tells JavaScript to use the Dog constructor function to create a new instance (i.e, a Dog object of the Dog class. This process, in OO lingo, is called instantiation. You might say "I instantiated a Dog." I'd know what you meant, even if your significant other didn't. Back to our test script, the arguments to the Dog constructor function define this particular dog — in this case, Miki, born on February 11, 2006.
The second block of the test script simply computes the dog's age (in years). In the next post, I'll show you how you can build this code right into the Dog class, so you can use it much more conveniently.
The last block of the test script demonstrates that we do, in fact, have a Dog object, constructed by the Dog class constructor function. That's what the instanceof operator is actually doing...
- 1,533 Views
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.