Der JavaScript-Fehler ""use strict"
nicht erlaubt in Funktion" tritt auf, wenn eine "use strict"
-Anweisung am Anfang einer Funktion verwendet wird, die Standardparameter, Rest-Parameter oder Destrukturierungs-Parameter hat.
SyntaxError: Illegal 'use strict' directive in function with non-simple parameter list (V8-based) SyntaxError: "use strict" not allowed in function with default parameter (Firefox) SyntaxError: "use strict" not allowed in function with rest parameter (Firefox) SyntaxError: "use strict" not allowed in function with destructuring parameter (Firefox) SyntaxError: 'use strict' directive not allowed inside a function with a non-simple parameter list. (Safari)Fehlertyp Was ist schiefgelaufen?
Eine "use strict"
-Anweisung ist am Anfang einer Funktion geschrieben, die einen der folgenden Parameter hat:
Eine "use strict"
-Anweisung ist am Anfang solcher Funktionen laut der ECMAScript-Spezifikation nicht erlaubt.
In diesem Fall hat die Funktion sum
Standardparameter a=1
und b=2
:
function sum(a = 1, b = 2) {
// SyntaxError: "use strict" not allowed in function with default parameter
"use strict";
return a + b;
}
Wenn die Funktion im Strict Mode sein soll und das gesamte Skript oder die umgebende Funktion ebenfalls im Strict Mode sein kann, können Sie die "use strict"
-Anweisung auÃerhalb der Funktion verschieben:
"use strict";
function sum(a = 1, b = 2) {
return a + b;
}
Funktionsausdruck
Ein Funktionsausdruck kann eine andere Lösung verwenden:
const sum = function sum([a, b]) {
// SyntaxError: "use strict" not allowed in function with destructuring parameter
"use strict";
return a + b;
};
Dies kann in den folgenden Ausdruck umgewandelt werden:
const sum = (function () {
"use strict";
return function sum([a, b]) {
return a + b;
};
})();
Pfeilfunktion
Wenn eine Pfeilfunktion auf die Variable this
zugreifen muss, können Sie die Pfeilfunktion als umgebende Funktion verwenden:
const callback = (...args) => {
// SyntaxError: "use strict" not allowed in function with rest parameter
"use strict";
return this.run(args);
};
Dies kann in den folgenden Ausdruck umgewandelt werden:
const callback = (() => {
"use strict";
return (...args) => this.run(args);
})();
Siehe auch
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4