We have identified in
JavaScript
some of the elements that must appear in any powerful programming language:
-
Numbers and arithmetic operations are primitive data and
functions.
-
Nesting of combinations provides a means of combining operations.
-
Constant declarations that associate names with values provide a
limited means of abstraction.
Now we will learn about
function declarations,
a much more powerful abstraction technique by which a compound
operation can be given a name and then referred to as a unit.
ここまでに、
JavaScript
において、強力なプログラミング言語に必要な要素のいくつかを確認しました:
-
数と算術演算はプリミティブなデータと
関数
です。
-
組み合わせの入れ子は、操作を結合する手段を提供します。
-
名前と値を関連付ける定数宣言は、限定的な抽象化の手段を提供します。
次に、
関数宣言
について学びます。
これは、複合操作に名前を付けて一つの単位として参照できるようにする、
はるかに強力な抽象化の技法です。
We begin by examining how to express the idea of
squaring.
We might say,
To square something, take it times itself.
This is expressed in our language as
まず、「二乗する」という考えをどのように表現するかを見てみましょう。
何かを二乗するには、それにそれ自身を掛ける。
と言えるでしょう。これは私たちの言語では次のように表現されます:
function square(x) {
return x * x;
}
We can understand this in the following way:
これは次のように理解できます:
function square( x ) { return x * x; }
// ^ ^ ^ ^ ^ ^ ^
// To square something, take it times itself.
We have here a
compound function,
which has been given the name square. The
function
represents the operation of multiplying something by itself. The thing to
be multiplied is given a local name, x,
which plays the same role that a pronoun plays in natural language.
Evaluating the
declaration
creates this compound
function
and associates it with the name
square.
ここでは、square という名前が付けられた
複合関数
ができました。この
関数
は、何かにそれ自身を掛ける操作を表しています。
掛けられるものにはローカルな名前 x が付けられており、
これは自然言語における代名詞と同じ役割を果たします。
この
宣言
を評価すると、複合
関数
が作成され、名前
square に関連付けられます。
The simplest form of a function declaration
is
関数宣言の最も単純な形式
は次の通りです:
function $name$($parameters$) { return $expression$; }
The
$name$
is a symbol to be associated with the
function
definition in the environment.
The
$parameters$
are the names used within the body of the
function
to refer to the
corresponding arguments of the
function.
$name$
は、環境において
関数
の定義に関連付けられるシンボルです。
$parameters$
は、
関数
の本体内で、対応する
関数
の引数を参照するために使われる名前です。
The $parameters$
are grouped within
parentheses and separated by commas, as they will be in an application
of the function being declared.
In the simplest form, the
body of a function declaration is a single
return statement,
which consists of the keyword
return
followed by the return expression
that will yield the value of the function application, when the
parameters are replaced by the actual arguments to which the function
is applied. Like constant declarations and expression statements,
return statements
end with a semicolon.
$parameters$は、宣言される関数の適用時と同じように、
括弧の中にカンマ区切りでまとめられます。
最も単純な形式では、関数宣言の
本体は単一の
return 文です。
return 文は、キーワード
return
の後にreturn 式が続く形になっています。
この return 式は、
パラメータが関数に渡される実際の引数に置き換えられたときに、
関数適用の値を返します。
定数宣言や式文と同様に、return 文も
セミコロンで終わります。
Having declared square,
we can now use it in a
function application expression, which we turn into a statement
using a semicolon:
square を宣言したので、
関数適用式で使えるようになりました。
セミコロンを付けて文にします:
Function applications are—after operator
combinations—the second kind of combination of
expressions into larger expressions that we encounter.
The general form of a function application is
関数適用は、演算子の組み合わせに次いで、
式をより大きな式へと組み合わせる2番目の方法です。
関数適用の一般的な形式は次の通りです:
$function$-$expression$($argument$-$expressions$)
where the
$function$-$expression$
of the application specifies
the function to be applied to the comma-separated
$argument$-$expressions$.
To evaluate a function application, the interpreter follows
a procedure
quite similar to the procedure for operator combinations described in
section
[1.1.3].
- To evaluate a function application, do the following:
-
Evaluate the subexpressions of the application, namely
the function expression and the argument expressions.
-
Apply the function that is the value of the function expression
to the values of the argument expressions.
ここで、適用の
$function$-$expression$
は、カンマで区切られた
$argument$-$expressions$
に適用する関数を指定します。
関数適用を評価するために、インタプリタは
[1.1.3]節で述べた
演算子の組み合わせの手順とよく似た手順に従います。
- 関数適用を評価するには、次のようにします:
-
適用の部分式、すなわち関数式と引数式を評価する。
-
関数式の値である関数を、引数式の値に適用する。
Here, the argument expression is itself a compound expression,
the operator combination 2 + 5.
ここで、引数式はそれ自体が複合式、
つまり演算子の組み合わせ 2 + 5 です。
Of course function application expressions can also serve as argument expressions.
もちろん、関数適用式を引数式として使うこともできます。
We can also use square
as a building block in defining other
functions.
For example, $x^2 +y^2$ can be expressed as
square を部品として使い、他の
関数
を定義することもできます。
例えば、$x^2 +y^2$ は次のように表現できます:
We can easily
declare
a
function
sum_of_squares
that, given any two numbers as arguments, produces the
sum of their squares:
2つの数を引数として受け取り、それらの二乗の和を求める
関数
sum_of_squares
を簡単に
宣言
できます:
function sum_of_squares(x, y) {
return square(x) + square(y);
}
Now we can use
sum_of_squares
as a building block in constructing further
functions:
今度は
sum_of_squares
を部品として、さらに別の
関数
を構築できます:
function f(a) {
return sum_of_squares(a + 1, a * 2);
}
In addition to compound functions, any JavaScript environment provides
primitive functions that are built into the interpreter or loaded
from libraries.
Besides the primitive functions provided by the operators,
the JavaScript environment used in this book includes
additional primitive functions
such as the function
math_log,
which computes the natural logarithm of its argument.
のような追加のプリミティブ関数が含まれています。
These additional primitive functions are used in exactly the same way as
compound functions; evaluating the application
math_log(1) results in the number 0.
Indeed, one could not tell by looking at the definition of
sum_of_squares given above whether
square was built into the
interpreter, loaded from a library, or defined as a compound function.
これらの追加のプリミティブ関数は、
複合関数とまったく同じ方法で使用されます。
例えば、適用 math_log(1) を評価すると数 0 になります。
実際、上で示した
sum_of_squares の定義を見ただけでは、
square がインタプリタに組み込まれているのか、
ライブラリから読み込まれたのか、複合関数として定義されたのかを
見分けることはできません。