aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/javascript/reference/errors/strict_non_simple_params/index.html
blob: fc033a3b765dc6690d8b89d8b022f92a0d39a18c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
---
title: 'SyntaxError: "use strict" not allowed in function with non-simple parameters'
slug: Web/JavaScript/Reference/Errors/Strict_Non_Simple_Params
tags:
  - Errors
  - JavaScript
  - TypeError
translation_of: Web/JavaScript/Reference/Errors/Strict_Non_Simple_Params
---
<div>{{jsSidebar("Errors")}}</div>

<h2 id="メッセージ">メッセージ</h2>

<pre class="syntaxbox">Firefox:
SyntaxError: "use strict" not allowed in function with default parameter
SyntaxError: "use strict" not allowed in function with rest parameter
SyntaxError: "use strict" not allowed in function with destructuring parameter

Chrome:
SyntaxError: Illegal 'use strict' directive in function with non-simple parameter list
</pre>

<h2 id="Error_type">エラーの種類</h2>

<p>{{jsxref("SyntaxError")}}</p>

<h2 id="何がうまくいかなかったのか?">何がうまくいかなかったのか?</h2>

<p>次の引数のうちいずれかを持つ関数の先頭に <code>"use strict"</code> ディレクティブが書かれています:</p>

<ul>
 <li>{{jsxref("Functions/Default_parameters", "Default parameters", "", 1)}}</li>
 <li>{{jsxref("Functions/rest_parameters", "Rest parameters", "", 1)}}</li>
 <li>{{jsxref("Operators/Destructuring_assignment", "Destructuring parameters", "", 1)}}</li>
</ul>

<p>ECMAScript 仕様に則って、このような関数の先頭では <code>"use strict"</code> を使用できません。</p>

<h2 id="例"></h2>

<h3 id="Function_ステートメント">Function ステートメント</h3>

<p>このケースでは、関数 <code>sum</code> は既定値を持つ引数 <code>a=1</code><code>b=2</code> を持っています:</p>

<pre class="brush: js example-bad">function sum(a=1, b=2) {
  // SyntaxError: "use strict" not allowed in function with default parameter
  "use strict";
  return a + b;
}
</pre>

<p>関数を <a href="/ja/docs/Web/JavaScript/Reference/Strict_mode">strict モード</a>にしたい、かつスクリプト全体、またはエンクロージャー関数が strict モードになってもよいなら、<code>"use strict"</code> ディレクティブを関数の外側に移動できます:</p>

<pre class="brush: js example-good">"use strict";
function sum(a=1, b=2) {
  return a + b;
}
</pre>

<h3 id="Function_式">Function 式</h3>

<p>function 式では、別の回避策をとることができます:</p>

<pre class="brush: js example-bad">var sum = function sum([a, b]) {
  // SyntaxError: "use strict" not allowed in function with destructuring parameter
  "use strict";
  return a + b;
};
</pre>

<p>これは、次の式に変換できます:</p>

<pre class="brush: js example-good">var sum = (function() {
  "use strict";
  return function sum([a, b]) {
    return a + b;
  };
})();
</pre>

<h3 id="アロー関数">アロー関数</h3>

<p>アロー関数が <code>this</code> 変数にアクセスする必要がある場合、アロー関数をエンクロージャー関数として使用できます:</p>

<pre class="brush: js example-bad">var callback = (...args) =&gt; {
  // SyntaxError: "use strict" not allowed in function with rest parameter
  "use strict";
  return this.run(args);
};
</pre>

<p>これは、次の式に変換できます:</p>

<pre class="brush: js example-good">var callback = (() =&gt; {
  "use strict";
  return (...args) =&gt; {
    return this.run(args);
  };
})();
</pre>

<h2 id="関連項目">関連項目</h2>

<ul>
 <li>{{jsxref("Strict_mode", "Strict mode", "", 1)}}</li>
 <li>{{jsxref("Statements/function", "function statement", "", 1)}}</li>
 <li>{{jsxref("Operators/function", "function expression", "", 1)}}</li>
 <li>{{jsxref("Functions/Default_parameters", "Default parameters", "", 1)}}</li>
 <li>{{jsxref("Functions/rest_parameters", "Rest parameters", "", 1)}}</li>
 <li>{{jsxref("Operators/Destructuring_assignment", "Destructuring parameters", "", 1)}}</li>
</ul>