Constructor
new Symbol(text)
When constructing a symbol, we must provide the text that defines it.
This will typically be the Unicode text for the symbol itself, such as
"x"
or "1"
, or the Unicode character for the Greek letter pi. But
there is no formal requirement that the text stored in the symbol be
exactly the text that would be used to represent the symbol in a
typical mathematical document. There is not even a requirement that
the text have any particular form, except that it be a nonempty string.
Parameters
-
text
String
any nonempty string to be used as the text for this symbol. If this is not a string, it will be converted into one with the
String
constructor in JavaScript. If that returns an empty string, it will be treated as"undefined"
instead.
Source
Classes
Methods
insertChild()
Symbols are supposed to be the atomic type of Expression. Thus we override here the default behavior of the insertChild() member of the MathConcept class, making it now do nothing. Since all other child insertion functions (such as pushChild(), etc.) rely internally on insertChild(), this effectively makes it impossible to add children to a Symbol instance.
Source
text() → {String}
A Symbol never changes its text. To have a new Symbol, just construct a new one with the new text, rather than trying to re-use an old one and change its text. Consequently, this function returns the text given at the time the Symbol was constructed.
Returns
-
String
the text given at construction time
Source
toString() → {String}
This method overrides the implementation in the MathConcept class, which creates LISP-style S-expressions. Here we handle the atomic case by writing the name of the symbol instead.
Returns
-
String
the string representation of this Symbol, which is just its text()
Source
value() → {*}
The original value()
function was implemented in the Expression class, but as a pure virtual method, meaning that it
defers its implementation to subclasses. Here, we add support for the
following conventions:
- A Symbol with the
"evaluate as"
attribute set to"integer"
will have avalue()
equal to the result of parsing the Symbol's text() content as an integer, using the standard JavaScriptparseInt()
function. This includes ignoring nonsense at the end of the string, returning the inital number only. Nan will be returned if the string does not even begin with an integer. - A Symbol with the
"evaluate as"
attribute set to"real"
will have avalue()
equal to the result of parsing the Symbol's text() content as a real number, using the standard JavaScriptparseFloat()
function. Note that this supports not only standard decimal notation, but also the text"Infinity"
and scientific notation of the form1.2e3
or1.2E3
. Spaces are permitted and nonsense at the end of the string is ignored. NaN will be returned if the string does not even begin with a float. - A Symbol with the
"evaluate as"
attribute set to"string"
will have avalue()
equal to its text().
Returns
-
*
The value of the Symbol, as documented above, or undefined if none of the above cases applies