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

m
no edit summary
mNo edit summary
mNo edit summary
Line 46: Line 46:


==== Terminology ====
==== Terminology ====
:; Constant:a value that has a string (text) value or a numeric (number) value.
:; Constant: A value that has a string (text) value or a numeric (number) value.
<small>
<small>
::; String literal:"This is a string of textual characters"
::; String literal: <code>"This is a string of textual characters"</code>
::; String constant: Sol or PlanetMars
::; String constant: <code>Sol</code> or <code>PlanetMars</code>&nbsp;&nbsp;&nbsp;&nbsp;// By convention, constants are distinguished from variables by starting with a capital letter
::; Number literal: 123 or 314e-2 or -28.5
::; Number literal: 123 or 314e-2 or -28.5&nbsp;&nbsp;&nbsp;&nbsp;// The middle one is in scientific format
::; Numeric constant: Age or ListPrice
::; Numeric constant: <code>EverestHeight</code> or <code>C**2</code>&nbsp;&nbsp;&nbsp;&nbsp;// The height in feet of Mount Everest or the speed of light squared
::; Symbol constant:a name representing a constant value, like ''pi'' or ''surname''
::; Symbol constant: A name representing a constant value, like <code>pi</code> or <code>surname</code>
</small>
</small>
:; Variable: a name representing a value that may change over time, like ''sum'', ''currentValue'', or ''orderDate''
:; Variable: A name representing a value that may change over time, like <code>sum</code>, <code>currentValue</code>, or <code>orderDate</code>
:; Operator: one or more characters that represent an expression, like '''+''' for ''plus'' or '''-''' for ''minus'' or '''*''' for ''multiplied by'' or '''/''' for ''divided by''.
:; Operator: One or more characters that represent an expression, like <code>+</code> for ''plus'' or <code>-</code> for ''minus'' or <code>*</code> for ''multiplied by'' or <code>/</code> 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''.
:: Some operators are made up of other operators used in combination, like <code><=</code> for ''less than or equal to'' or <code>&&</code> for ''and also''. One operator is ''3'' characters long: <code>===</code> means ''is equal to & is of the same type as''. JavaScriptd is ''loosely typed'', so its types are very general, like Number, String, or Date.
 
:: in x++, ++ is a ''unary'' operator.
:: in b - 5, - is a ''binary'' operator.
:: in canDrive = age > 16 ? 'yes' : 'no', ? and : are used like if and else together as the only ''ternary'' operator in JavaScript &mdash; the ''conditional'' operator. The > is a binary operator that returns ''true'' or ''false''. If the result is true (the age is over 16), then canDrive will be true. If it is false, canDrive will be false. It is good for cases where something is this or else that &mdash; one or the other, plain & simple. Since it is plain & simple, it is (perhaps) appropriate to simplify the conditional as well, with ? and :.


:: In <code>x++</code>, <code>++</code> is a ''unary'' operator.
:: In <code>b - 5</code>, <code>-</code> is a ''binary'' operator.
:: In <code>canDrive = age > 16 ? 'yes' : 'no'</code> &mdash; <code>?</code> and <code>:</code> are used like <code>if</code> & <code>else</code> together as the only ''ternary'' operator in JavaScript &mdash; the ''conditional'' operator. The <code>></code> is a binary operator that returns ''true'' or ''false''. If the result is true (the age is over 16), then canDrive will be true. If it is false, canDrive will be false. It is good for cases where something is this or else that &mdash; one or the other, plain & simple. Since it is plain & simple, it is (perhaps) appropriate to simplify the conditional as well, with <code>?</code> and <code>:</code>.


== Shared script ==
== Shared script ==