SlightlyLoony's blog

JavaScript Tips and Tricks: The Three Flavors of Strings...

A string is a string, right? Ah, if only 'twas so!

JavaScript all by itself has two different kinds of strings: there's the primitive “string” (with a lower case ‘s’) and then there's the “String” class (which you can create instances of. For example:

var x = 'this is a primitive string';
var y = new String('this is a String instance');
gs.log(x + ': ' + typeof x);   // this will show typeof 'string'
gs.log(y + ': ' + typeof y);   // this will show typeof 'object'

In the code above, the variable x has a primitive string and the variable y has a String instance. In practical usage, the two behave nearly identically – with the obvious exception of the result of the typeof operator.

But what about the third flavor?

JavaScript Tips and Tricks: The Two Flavors of Undefined...

In JavaScript, “undefined” means two distinctly different things:

  1. A property that doesn't exist.
  2. A property that has the “undefined” value assigned to it.

Huh? Aren't those two ways of saying the same thing? Well…not exactly, and the differences can lead to some unexpected results. Consider this example:

JavaScript Tips and Tricks: What's Up with Packages?

Several customers have asked me about the odd-looking “Packages...” invocations they see sometimes in server-side JavaScript (like business rules or script includes). For instance, you might find a piece of code like this:

var a = getCITypes();
JSUtil.logObject(a);

function getCITypes() {
    var hs = new Packages.java.util.HashSet();
    var gr = new GlideRecord('cmdb_ci');
    gr.query();
    while (gr.next())
        hs.add('' + gr.sys_class_name);
    var it = hs.iterator();
    var answer = [];
    while (it.hasNext())
        answer.push('' + it.next());
   return answer;
}

This code will return (very inefficiently!) a list of all the types of CIs that are actually in your CMDB. But what the heck is that funny-looking “Packages.java.util.HashSet()” all about?

JavaScript Tips and Tricks: What's Up with the Functions?

In a conversation I had with a customer the other day, she asked me why she saw so many examples of business rules written in functions, like this:

doImportantStuff();

function doImportantStuff() {
    var x = 15;
    var y = 22.3;
    var z = Math.pow(15, 22.3);
    current.answer = z;
}

Well, leaving aside the questionable wisdom of ever raising 15 to the 22.3rd power, that's a good question – and the answer lies both in the nature of JavaScript and of the Service-now.com platform:

JavaScript Tips and Tricks: JSUtil...

Did you know that your Service-now instance comes complete with a handy-dandy set of commonly used JavaScript utility methods? These are in a script include named "JSUtil", and you can easily use its methods in your scripts.

For example, there's a method named "nil()" (along with "notNil()" that lets you easily test whether a value is null, undefined, or an empty string. You might use it in your own code it like this:

function myFunc(val) {
    if (JSUtil.nil(val))
        return null;
    return 'My prefix: ' + val;
}

That's all there is to it! Take a look at the script include to see what's in it – all the methods are documented. And if you think of another method that ought to be in there, please add a comment describing what you'd like to see...

Deep Dive: JavaScript Inner Functions...

Here's a simple and very useful feature of JavaScript that you may not be aware of: functions may be nested, one inside another. Here's a simple example, in a script you can run on your instance:

gs.log('Hex value of 11259375 is: ' + hex(11259375)); function hex(value) { var rem = value; var result = ''; while (rem > 0) { var r = rem % 16; rem = Math.floor(rem/16); result = hexChar(r) + result; } return result; function hexChar(charValue) { return '0123456789ABCDEF'.charAt(charValue & 0xFF); } }

Knowledge10 Bleg...

Our Knowledge10 conference is coming up in just a couple of months! I'll be there, and I hope to see you there as well. But whether you're attending or not, we'd like to hear your thoughts on the best use of our two hour Discovery lab.

We've had several ideas ourselves – a troubleshooting primer, an implementation primer, advanced classifications, building custom probes and sensors, and so on. But these are products of our fevered and slightly loony imaginations. What we'd really like to know is this: what would you find most valuable? Please let us know your thoughts in the comments to this post!

Unexpected JavaScript...

Every once in a while, I run across something completely unexpected in JavaScript. This morning was one of those times.

In my code, I had the equivalent of this:

var x = 0;
if ('' == x)
    gs.log('What the???');
else
    gs.log('Ah, that was expected!');

Wanna take a guess which statement was logged?

Object-Oriented JavaScript in Our Stuff...

Many of the kinds of things y'all want to customize in Discovery (especially sensors and CI consolidation) involve some programming in JavaScript. Even if you're already familiar with JavaScript, you may run into something new when you look at our existing code for examples: object-oriented programming in JavaScript. Then on top of that, we've extended JavaScript a bit with a some things specific to Service-now.com.

There is lots of great material already available for object-oriented JavaScript. If you're seriously interested in becoming a JavaScript expert, I highly recommend JavaScript: the Definitive Guide, by David Flanagan. It's simply the best reference out there I've run into. Alternatively, here are some good starting points on the Intertubes: Douglas Crockford's excellent JavaScript essays, Mike Koss' text, and JavaScriptKit's tutorial.

But to learn about Service-now.com's extensions, read on...

Deep Dive: Replaceable Probe Parameters...

Have you noticed that some probes have commands with odd-looking “words” in them? For example, the MySQL - Configuration probe has a command cat ${filename}, and the Linux - Disk probe has a command sh ${file:hd.sh}. What's up with those dollar signs and squiggly braces?

Well, those are called replaceable parameters, and they're actually quite useful. Read on if you'd like to know more about them...

Syndicate content