Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

SyntaxError: "use strict" not allowed in function with "x" parameter

이 글은 기술 검토가 필요합니다. 도울을 줄 수 있는 방법을 살펴보세요.

이 글은 편집 검토가 필요합니다. 도울을 줄 수 있는 방법을 살펴보세요.

이 문서는 아직 자원 봉사자들이 한국어로 번역하지 않았습니다. 함께 해서 번역을 마치도록 도와 주세요!

Message

SyntaxError: "use strict" not allowed in function with "x" parameter (Firefox)
SyntaxError: Illegal 'use strict' directive in function with non-simple parameter list (Chrome)

Error type

SyntaxError.

What went wrong?

"use strict" directive appears at the top of a function that has one of the follow parameters.

"use strict" is not allowed at the top of such function.

Examples

Function statement

In this case, the function "sum" has default parameters "a=1" and "b=2".

function sum(a=1, b=2) {
  // SyntaxError: "use strict" not allowed in function with default parameter
  "use strict";
  return a + b;
}

If the function should be in strict mode, and the entire script or enclosing function is also okay to be in strict mode, you can move "use strict" outside.

"use strict";
function sum(a=1, b=2) {
  return a + b;
}

Function expression

Function expression can use yet another workaround.

var sum = function sum([a, b]) {
  // SyntaxError: "use strict" not allowed in function with destructuring parameter
  "use strict";
  return a + b;
};

This can be converted into following expression.

var sum = (function() {
  "use strict";
  return function sum([a, b]) {
    return a + b;
  };
})();

Arrow function

If it's an arrow function and needs to access this variable, you can use arrow function as the enclosing function.

var callback = (...args) => {
  // SyntaxError: "use strict" not allowed in function with rest parameter
  "use strict";
  return this.run(args);
};

This can be converted into following expression.

var callback = (() => {
  "use strict";
  return (...args) => {
    return this.run(args);
  };
})();

See also

문서 태그 및 공헌자

 이 페이지의 공헌자: nbp, arai
 최종 변경: nbp,