aboutsummaryrefslogtreecommitdiff
path: root/files/de/web/javascript/reference/errors/strict_non_simple_params/index.html
blob: 285ae0b7e7da2427e41c76e41e1b1315f1ee161f (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
112
---
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
original_slug: Web/JavaScript/Reference/Fehler/Strict_Non_Simple_Params
---
<div>{{jsSidebar("Errors")}}</div>

<h2 id="Fehlermeldung">Fehlermeldung</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="Fehlertyp">Fehlertyp</h2>

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

<h2 id="Was_ist_falsch_gelaufen">Was ist falsch gelaufen?</h2>

<p>Eine <code>"use strict"</code> Direktive steht am Anfang einer Funktion, die einen der folgende Parameter hat:</p>

<ul>
 <li>{{jsxref("Functions/Default_parameters", "Standardparameter", "", 1)}}</li>
 <li>{{jsxref("Functions/rest_parameters", "Rest Parameter", "", 1)}}</li>
 <li>{{jsxref("Operators/Destructuring_assignment", "Destrukturierte Parameter", "", 1)}}</li>
</ul>

<p>Eine <code>"use strict"</code> Direktive ist am Anfang solcher Funktionen durch die ECMAScript Spezifikation nicht erlaubt.</p>

<h2 id="Beispiele">Beispiele</h2>

<h3 id="Funktionsstatement">Funktionsstatement</h3>

<p>In diesem Fall hat die Funktion <code>sum</code> zwei Standardparameter <code>a=1</code> und <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>Wenn die Funktion im <a href="/de/docs/Web/JavaScript/Reference/Strict_mode">Strict Mode</a> sein soll und das Skript oder die umschließende FUnktion auch für den Strict Mode in Ordnung ist, kann man die <code>"use strict"</code> Direktive nach außen verschieben:</p>

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

<h3 id="Funktionsausdruck">Funktionsausdruck</h3>

<p>Bei eine Funktionsausdruck kann ein andere Workaround genutzt werden:</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>Dieses kann zu folgendem Ausdruck konvertiert werden:</p>

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

<h3 id="Pfeilfunktionen">Pfeilfunktionen</h3>

<p>Wenn eine Pfeilfunktion auf die <code>this</code> Variable zugreift, so kann eine umschließende Pfeilfunktion benutzt werden:</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>Dieses kann zu folgendem Ausdruck konvertiert werden:</p>

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

<h2 id="Siehe_auch">Siehe auch</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>