One easy way to get started at programming is to examine some typical
interactions with an interpreter for the
JavaScript language.
You type
a statement,
and the interpreter responds by displaying the
result of its evaluating that
statement.
プログラミングを始める簡単な方法の一つは、
JavaScript 言語
のインタプリタとの典型的なやり取りを調べることです。
文を
入力すると、インタプリタはその
文
を評価した結果を表示します。
In this edition, the mouse click on
JavaScript statements with dark background is
programmed in such a way that a JavaScript interpreter is
displayed, which can evaluate the
statement and display the resulting value.
By the way, the program that makes the mouse click
on a JavaScript statement display the interpreter is itself
written in JavaScript; it is called the script for
the mouse click. Such scripts were a central objective in
the original design of JavaScript.
One kind of statement you might type is an
expression statement, which consists of an
expression followed by a semicolon.
One kind of primitive expression is a number.
入力する文の一つの種類は、式の後にセミコロンが続く式文です。
プリミティブな式の一つの種類は数です。
(More precisely, the expression that you type consists of the numerals that
represent the number in base 10.)
(より正確には、入力する式は10進法で数を表す数字で構成されます。)
If you ask our script to evaluate the expression statement
式文を評価するようスクリプトに依頼すると
by clicking it, it will respond by displaying a JavaScript interpreter
with the option
to evaluate the statement by pressing a Run
button.
Click on the primitive expression statement, and see what happens!
クリックすると、JavaScript インタプリタが表示され、
Run
ボタンを押して文を評価できます。
プリミティブな式の文をクリックして、何が起こるか見てみましょう!
Expressions representing numbers may be combined with
operators
(such
as +
or *) to form a
compound expression that represents the
application of a corresponding primitive
function to those numbers. For example,
evaluate
any of the following expression statements
by clicking on it:
数を表す式は、演算子
(+
や *など)と組み合わせて、
対応するプリミティブ関数をそれらの数に適用することを表す複合式を形成できます。
例えば、
以下の式文のいずれかをクリックして評価してみてください:
Expressions such as these, which contain other expressions
as components, are called combinations.
Combinations that are formed by an
operator symbol in the middle, and
operand expressions to the left and right of it,
are called
operator combinations.
The value of an operator combination is
obtained by applying the function specified by the operator to the
arguments that are the values of the operands.
他の式を構成要素として含むこのような式は、組み合わせと呼ばれます。
中央に
演算子記号があり、その左右に
オペランド式がある組み合わせは、
演算子の組み合わせと呼ばれます。
演算子の組み合わせの値は、演算子が指定する関数を、オペランドの値である引数に適用することで得られます。
The convention of placing the operator between the operands is
known as
infix notation. It follows the mathematical notation that
you are most likely familiar with from school and everyday life.
As in mathematics, operator combinations can be nested, that
is, they can have operands that
themselves are operator combinations:
演算子をオペランドの間に置く慣習は、
中置記法として知られています。
これは学校や日常生活でおなじみの数学的表記法に従っています。
数学と同様に、演算子の組み合わせは入れ子にできます。
つまり、オペランド自体が
演算子の組み合わせであることができます:
As usual,
parentheses are used to group operator combinations in order
to avoid ambiguities. JavaScript also follows the usual conventions
when parentheses are omitted: multiplication and division bind more
strongly than addition and subtraction. For example,
いつも通り、
曖昧さを避けるために括弧を使って演算子の組み合わせをグループ化します。
JavaScript は括弧が省略された場合も通常の慣習に従います:
乗算と除算は加算と減算よりも強く結合します。例えば、
stands for
は次と同じ意味です:
We say that * and
/ have
higher precedence
than + and
-. Sequences of additions and
subtractions are read from left to right, as are sequences of
multiplications and divisions. Thus,
* と
/ は
+ と
- よりも
高い優先順位を持つと言います。
加算と減算の列は左から右に読まれ、乗算と除算の列も同様です。したがって、
stands for
は次と同じ意味です:
We say that the operators
+,
-,
* and
/ are
left-associative.
演算子
+、
-、
*、
/ は
左結合であると言います。
There is no limit (in principle) to the depth of such nesting and to the
overall complexity of the expressions that the JavaScript interpreter
can evaluate. It is we humans who might get confused by still relatively
simple expressions such as
このような入れ子の深さや、JavaScript インタプリタが評価できる式の全体的な複雑さには
(原理的には)制限がありません。まだ比較的単純な式でも混乱するのは私たち人間の方です。
例えば、
3 * 2 * (3 - 5 + 4) + 27 / 6 * 10;
which the interpreter would readily evaluate to be 57. We can help
ourselves by writing such an expression in the form
をインタプリタは難なく 57 と評価します。次のような形で式を書くことで理解しやすくなります:
3 * 2 * (3 - 5 + 4)
+
27 / 6 * 10;
to visually separate the major components of the expression.
こうすることで、式の主要な構成要素を視覚的に分離できます。
Even with complex expressions, the interpreter always operates in the
same basic cycle: It reads
a statement typed by the user,
evaluates the
statement,
and prints the result. This mode of operation is often expressed by saying
that the interpreter runs in a
read-evaluate-print loop.
Observe in particular that it is not necessary to explicitly instruct the
interpreter to print the value of the
statement.
複雑な式であっても、インタプリタは常に同じ基本サイクルで動作します:
ユーザーが入力した文を
読み取り、
文
を評価し、結果を表示します。
この動作モードは、インタプリタが
read-evaluate-print ループ
で動作すると表現されます。
特に注目すべきは、
文の値を表示するようインタプリタに明示的に指示する必要がないことです。