A critical aspect of a programming language is the means it provides
for using
names to refer to computational
objects, and our first such means are constants.
We say that the
name identifies a
constant
whose
value is the object.
プログラミング言語の重要な側面は、
計算オブジェクトを参照するために名前を使う手段を提供することです。
最初のそのような手段は定数です。
名前は、
定数
を識別し、その
値がそのオブジェクトであると言います。
In JavaScript, we name constants with
constant declarations.
JavaScript では、
定数宣言で定数に名前を付けます。
causes the interpreter to associate the value 2 with the
name size.
はインタプリタに値 2 を名前 size に関連付けさせます。
Once the name size
has been associated with the number 2, we can
refer to the value 2 by name:
名前 size が数 2 に関連付けられると、
名前で値 2 を参照できます:
The JavaScript interpreter needs to execute the constant
declaration for size
before the name size can be used
in an expression. In this online book, the statements that need to be
evaluated before a new statement are omitted for brevity. However,
to see and play with the program, you can click on it. The
program then appears in a new
browser tab, with the option Show Dependencies
.
Thus, as a result of clicking on
a new tab appears that contains the program, and after clicking
Show Dependencies
, you see:
const size = 2;
5 * size;
Here are further examples of the use of
const:
const
の使用例をさらに示します:
pi * radius * radius;
314.159
const circumference = 2 * pi * radius;
Constant
declaration
is our language's
simplest means of abstraction, for it allows us to use simple names to
refer to the results of compound operations, such as the
circumference computed above.
In general, computational objects may have very complex
structures, and it would be extremely inconvenient to have to remember
and repeat their details each time we want to use them. Indeed,
complex programs are constructed by building, step by step,
computational objects of increasing complexity. The
interpreter makes this step-by-step program construction particularly
convenient because name-object associations can be created
incrementally in successive interactions. This feature encourages the
incremental development and testing of programs and is largely
responsible for the fact that a
JavaScript
program usually consists of a large
number of relatively simple
functions.
定数
宣言
は、この言語の最も単純な抽象化の手段です。
上で計算した circumference のように、
複合操作の結果を単純な名前で参照できるようにしてくれます。
一般に、計算オブジェクトは非常に複雑な構造を持つことがあり、
使うたびにその詳細を覚えて繰り返さなければならないのは極めて不便です。
実際、複雑なプログラムは、複雑さを増していく計算オブジェクトを
段階的に構築することで作られます。
インタプリタは、名前とオブジェクトの関連付けを逐次的なやり取りの中で
段階的に作成できるため、このような段階的なプログラム構築を特に便利にします。
この特徴は、プログラムの
段階的な開発とテストを促進し、
JavaScript
のプログラムが通常、比較的単純な多数の
関数
で構成されることの大きな理由です。
It should be clear that the possibility of associating values with
names and later retrieving them means that the interpreter must
maintain some sort of memory that keeps track of the name-object
pairs. This memory is called the
environment
(more precisely the
program environment,
since we will see later that a
computation may involve a number of different
environments).
値を名前に関連付けて後で取り出せるということは、インタプリタが
名前とオブジェクトの組を追跡するある種のメモリを維持しなければならないことを
意味するのは明らかです。このメモリは
環境
(より正確には
プログラム環境
)と呼ばれます。
後で見るように、計算は多くの異なる環境を含むことがあります。