Scripting: Difference between revisions

Jump to navigation Jump to search
mNo edit summary
mNo edit summary
Line 9: Line 9:


=== The '''[[JavaScript field]]''' type ===
=== The '''[[JavaScript field]]''' type ===
Like the Memento Calculation field type that game before it, the JavaScript field exists to produce ("return") a value calculated with an ''expression'', which is a formula consisting of ''constants'', ''variables'', ''operators'', and possibly ''statements''.
Like the Memento Calculation field type that came before it, the JavaScript field exists to produce ("return") a value calculated with an ''expression'' or formula. JavaScript fields use a somewhat simplified form of Memento JavaScript. For instance, it assumes the current entry for its context, while in full Memento JavaScript, the script must locate & provide the entry to operate on. While it can access any field in any library the current library is linked to, it doesn't have the complexity to access any field in any entry in any library, as you can in full Memento JavaScript. See [[How:Write scripts in JavaScript for Memento#JavaScript field]] for a detailed discussing of the JavaScript field.


==== Example simple expression scripts ====
==== Example JavaScript field usage ====
:;123: the value ''a hundred and twenty-three''
* The field() function gets the value of the field from Memento.
:;"Memento": the characters ''M e m e n t o'' as a string
* Anything after "//" is ignored, so you can use it for comments.
:; 2 * currentWeight: If currentWeight is 23 lbs, then 46 lbs
* For numbers, x * y means the product of x times y.
:; (hours * 60) + 10:If hours is 5 and minutes is 10, then 310
* For numbers, x + y means the sum of x and y, while with strings of text, it means x concatenated with y.
 
:; Item price // on an invoice:field("Unit price") * field("Quantity")
==== Terminology ====
:; Amount for display // Final sum: field("Total") + " widgets"
:; Constant:a value that has a string (text) value or a numeric (number) value.
:; Limited // The capped amount: value = field("Value"); if (value <= StandardLimit) value else StandardLimit
<small>
::; String:"This is a string of textual characters"
::; Number:123 or 3.14 or -28.5
::; Symbol:a name representing a constant value, like ''pi'', ''x'', ''tareWeight'', or ''surname''
</small>
:; Variable:a name representing a value that may change over time, like ''sum'', ''currentValue'', or ''orderDate''
:; Operator: one or more characters that represent the value of other values that go together in some fashion to determine a result, like '''+''' for ''plus'' or '''-''' for ''minus'' or '''*''' for ''multiplied by'' or '''/''' for ''divided by''.
::Some operators are made up of other operators used in combination, like '''<=''' ''for less than or equal to'' or '''&&''' for ''and also''.


=== JavaScript scripting for [[Triggers|'''Memento Triggers''']] ===
=== JavaScript scripting for [[Triggers|'''Memento Triggers''']] ===