The Now Platform® Washington DC release is live. Watch now!

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
SlightlyLoony
Tera Contributor

find_real_file.pngIf you've done any programming at all in JavaScript, then you're familiar with functions (I hope!). Normally a function looks something like this nice little message formatting function:


var x = say('${1} had a ${2} just ${3}.', 'SlightlyLoony', 'nice glass of wine', 'last night');
gs.log(x);

function say(msg, parm1, parm2, parm3) {
var x = msg.replace(/\$\{1\}/g, parm1);
x = x.replace(/\$\{2\}/g, parm2);
x = x.replace(/\$\{3\}/g, parm3);
return x;
}

But what if we wanted a more flexible message formatter? In particular, one that would take any number of parameters, not just three?

The function below does exactly that:

var x = say('${1} had a ${2} just ${3}.', 'SlightlyLoony', 'nice glass of wine', 'last night');
gs.log(x);

function say() {
return arguments[0].replace(/\$\{([0-9]+)\}/g, replacer);

function replacer(pat, mat) {
return say.arguments[mat];
}
}

There are two things in here that may be new to you:
  1. The "arguments" variable: When any function is invoked, whatever arguments were passed in are available in the arguments array variable. The length of the arguments array tells you how many arguments were passed (this could be different on different function invocations). It doesn't matter if parameters are declared in the function definition; the arguments variable will be filled in exactly the same way regardless. The say() function assumes that the first argument (argument[0]) is the message pattern string, and that any subsequent arguments are parameters that might be used to replace elements in the message pattern string. The replace function invocation says "replace anything that looks like ${n} with the result of the replacer function (which is defined immediately below).
  2. The replacement function: The replacer function is invoked each time the replace method finds a string to replace. This function could have any name, of course. When the replacement function is invoked, the first parameter is the matched string (such as "${1}" in our example), and the following parameters are the contents of any capture groups (the things inside parentheses) in the matching regular expression. In the example there's only one capture group, which captures the number inside the ${n} pattern. The replacer function uses this value (mat in our example) as an index into the parameters to the say() function (note how we used say.arguments to get the arguments variable of the surrounding function rather than of our replacer function). So this replacement function simply returns the replacement parameter that matches the number in the message pattern string.

And voila! With really very little code, we have a completely general message formatting function that can take any number of parameters.

Functions with a variable number of argument useful in lots of places. The classic example is the general purpose sum() function:

var x = sum(15, 67, 345, 22, 9352,1756);
gs.log('And the answer is: ' + x);

function sum() {
var answer = 0;
for (var i = 0; i < arguments.length; i++)
answer += arguments;
return answer;
}


Add these to your bag of tricks!