19 February 2022
MDN doc about SyntaxError: "use strict" not allowed in function with non-simple parameters.
function df(any = "") {
"use strict";
}
Uncaught SyntaxError: "use strict" not allowed in function with default parameter
function ds({ any = "" }) {
"use strict";
}
// Uncaught SyntaxError: "use strict" not allowed in function with destructuring parameter
function rp(...any) {
"use strict";
}
// Uncaught SyntaxError: "use strict" not allowed in function with rest parameter
NodeJS issue filed (then closed) by Andrea Giammarchi (@webreflection), Unable to destructure arguments as generic key #42051.
Error message in Node.js reads, "SyntaxError: Unexpected eval or arguments in strict mode".
In the browser (Firefox) console, things are slightly different.
Example:
function A(arguments) {
"use strict";
}
// Uncaught SyntaxError: 'arguments' can't be defined or assigned to in strict mode code
However, if arguments is a destructured key, then the destructuring parameters message will appear instead.
function A({ arguments }) {
"use strict";
}
// Uncaught SyntaxError: "use strict" not allowed in function with destructuring parameter
Even if the function is defined in a nested context, inside another function with the "use strict" directive, the syntax error appears.
var A = (function() {
"use strict";
return function A({ arguments }) {
console.log(arguments);
}
})()
// Uncaught SyntaxError: 'arguments' can't be defined or assigned to in strict mode code
What if it's a field on an object passed in?
function A(o) {
"use strict";
console.log(Object(o).arguments);
}
A({ arguments: 9 });
// 9
The browser console is not strict by default, but ES6 classes (and modules) are, so we can use classes to demonstrate all the examples.
As a parameter name
class A {
noop(arguments) { }
}
// Uncaught SyntaxError: 'arguments' can't be defined or assigned to in strict mode code
Destructured parameter name
class A {
noop({ arguments }) { }
}
// Uncaught SyntaxError: 'arguments' can't be defined or assigned to in strict mode code
Assignment. As in var arguments
.
class A{
noop(any) {
var arguments = 1;
console.log(arguments)
}
}
// Uncaught SyntaxError: 'arguments' can't be defined or assigned to in strict mode code
Surprisingly, rest parameters pass, even though "classes are strict by default".
class A {
noop(...rest) { }
}
And so does the usual arguments reference.
class A {
noop(...rest) { console.log(arguments) }
}
new A().noop(1,2,3)
// Arguments { 0: 1, 1: 2, 2: 3, … }
Thus arguments
may still be referenced inside a class method as long as is not defined or assigned to.
That seems inconsistent.
more examples using classes needed