From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../reference/statements/function/index.html | 158 +++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 files/ja/web/javascript/reference/statements/function/index.html (limited to 'files/ja/web/javascript/reference/statements/function/index.html') diff --git a/files/ja/web/javascript/reference/statements/function/index.html b/files/ja/web/javascript/reference/statements/function/index.html new file mode 100644 index 0000000000..8db65175e7 --- /dev/null +++ b/files/ja/web/javascript/reference/statements/function/index.html @@ -0,0 +1,158 @@ +--- +title: 関数宣言 +slug: Web/JavaScript/Reference/Statements/function +tags: + - Function + - JavaScript + - Language feature + - Statement +translation_of: Web/JavaScript/Reference/Statements/function +--- +
{{jsSidebar("Statements")}}
+ +

関数宣言 (関数文) は、指定された引数を使用して関数を定義します。

+ +

また、 {{jsxref("Function")}} のコンストラクターと{{jsxref("Operators/function", "関数式", "", 1)}}を使用して関数を定義することもできます。

+ +
{{EmbedInteractiveExample("pages/js/statement-function.html","shorter")}}
+ + + +

構文

+ +
function name([param[, param,[..., param]]]) {
+   [statements]
+}
+
+ +
+
name
+
関数の名前です。
+
+ +
+
param {{optional_inline}}
+
関数に渡す引数の名前です。引数の最大数はエンジンによって異なります。
+
+ +
+
statements {{optional_inline}}
+
関数の本体を構成する文です。
+
+ +

解説

+ +

関数宣言で作成された関数は Function オブジェクトであり、Function オブジェクトのすべてのプロパティ、メソッド、動作を備えています。関数の詳細については {{jsxref("Function")}} を参照してください。

+ +

関数は式を使用して作成することもできます ({{jsxref("Operators/function", "関数式", "", 1)}}を参照)。

+ +

既定では、関数は undefined を返します。他の値を返すには、関数に返す値を指定する {{jsxref("Statements/return", "return")}} 文が必要です。

+ +

条件付きで作成される関数

+ +

関数は条件付きで宣言できます。つまり、関数文を if 文の中に入れ子にすることができますが、結果は実装によって一貫性がないので、このパターンを本番コードでは使用すべきではありません。条件付きの関数の作成には、代わりに関数式を使用してください。

+ +
var hoisted = "foo" in this;
+console.log(`'foo' name ${hoisted ? "is" : "is not"} hoisted. typeof foo is ${typeof foo}`);
+if (false) {
+  function foo(){ return 1; }
+}
+
+// In Chrome:
+// 'foo' name is hoisted. typeof foo is undefined
+//
+// In Firefox:
+// 'foo' name is hoisted. typeof foo is undefined
+//
+// In Edge:
+// 'foo' name is not hoisted. typeof foo is undefined
+//
+// In Safari:
+// 'foo' name is hoisted. typeof foo is function
+
+ +

真と評価される条件でも結果はまったく同じです。

+ +
var hoisted = "foo" in this;
+console.log(`'foo' name ${hoisted ? "is" : "is not"} hoisted. typeof foo is ${typeof foo}`);
+if (true) {
+  function foo(){ return 1; }
+}
+
+// In Chrome:
+// 'foo' name is hoisted. typeof foo is undefined
+//
+// In Firefox:
+// 'foo' name is hoisted. typeof foo is undefined
+//
+// In Edge:
+// 'foo' name is not hoisted. typeof foo is undefined
+//
+// In Safari:
+// 'foo' name is hoisted. typeof foo is function
+
+ +

関数宣言の巻き上げ

+ +

JavaScript の関数宣言は、それを囲む関数やグローバルスコープの先頭に巻き上げられ、関数を宣言する前に使うことができます。

+ +
hoisted(); // logs "foo"
+
+function hoisted() {
+  console.log('foo');
+}
+
+ +

{{jsxref("Operators/function", "関数式", "", 1)}}は巻き上げられないことに注意してください。

+ +
notHoisted(); // TypeError: notHoisted is not a function
+
+var notHoisted = function() {
+   console.log('bar');
+};
+
+ +

+ +

function の使用

+ +

以下のコードは、商品 a, b, c の販売個数が与えられた場合に、販売個数の合計を返す関数を宣言しています。

+ +
function calc_sales(units_a, units_b, units_c) {
+   return units_a * 79 + units_b * 129 + units_c * 699;
+}
+
+ +

仕様書

+ + + + + + + + + + + + +
仕様書
{{SpecName('ESDraft', '#sec-function-definitions', 'Function definitions')}}
+ +

ブラウザーの互換性

+ + + +

{{Compat("javascript.statements.function")}}

+ +

関連情報

+ + -- cgit v1.2.3-54-g00ecf