How:Write scripts in JavaScript for Memento: Difference between revisions

m
no edit summary
mNo edit summary
mNo edit summary
Line 68: Line 68:


For instance, trigger scripts are the most often used type of script, and in trigger scripts, it is very common and common practice to define a variable e to contain the current Entry object <code>var e = entry()</code>. Then, you can say <code>e.field()</code> instead of <code>entry().field()</code>. This not only saves keystrokes & makes the code more readable, but also runs a bit faster, because the <code>entry()</code> function is run only once in the script. This might be the first thing you'd think of to share. Of course, <code>entry()</code> is not called at all in ''library'' action scripts, for instance, so <code>e</code> will not be used in such scripts, but it doesn't hurt much at all, so it's OK that the code is shared also to these scripts.
For instance, trigger scripts are the most often used type of script, and in trigger scripts, it is very common and common practice to define a variable e to contain the current Entry object <code>var e = entry()</code>. Then, you can say <code>e.field()</code> instead of <code>entry().field()</code>. This not only saves keystrokes & makes the code more readable, but also runs a bit faster, because the <code>entry()</code> function is run only once in the script. This might be the first thing you'd think of to share. Of course, <code>entry()</code> is not called at all in ''library'' action scripts, for instance, so <code>e</code> will not be used in such scripts, but it doesn't hurt much at all, so it's OK that the code is shared also to these scripts.
Another powerful category of shared code is the shared function. For instance, consider the following function for shared use:
<pre>
// Function to return a formatted string given the following arguments:
// number: A number of type Number representing an amount of currency, such as a price
// decPlaces: The number of decimal places to which to format the number, defaulting to 2
// decSep: The character to use to separate the integral part of the amount from the
//        decimal fractional part, defaulting to "."
// thouSep:The character to use to separate the factors of one thousand from each other,
//        defaulting to ","
function formatMoney(number, decPlaces, decSep, thouSep) {
    decPlaces = isNaN(decPlaces = Math.abs(decPlaces)) ? 2 : decPlaces,
    decSep = typeof decSep === "undefined" ? "." : decSep;
    thouSep = typeof thouSep === "undefined" ? "," : thouSep;
    var sign = number < 0 ? "-" : "";
    var i = String(parseInt(number = Math.abs(Number(number) || 0).toFixed(decPlaces)));
    var j = (j = i.length) > 3 ? j % 3 : 0;
    return sign +
        (j ? i.substr(0, j) + thouSep : "") +
        i.substr(j).replace(/(\decSep{3})(?=\decSep)/g, "$1" + thouSep) +
        (decPlaces ? decSep + Math.abs(number - i).toFixed(decPlaces).slice(2) : "");
}
</pre>


== Trigger script ==
== Trigger script ==