Movatterモバイル変換


[0]ホーム

URL:


  1. 開発者向けのウェブ技術
  2. JavaScript
  3. JavaScript リファレンス
  4. JavaScript エラーリファレンス
  5. SyntaxError: "use strict" not allowed in function with non-simple parameters

このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docsコミュニティーについてもっと知り、仲間になるにはこちらから。

View in EnglishAlways switch to English

SyntaxError: "use strict" not allowed in function with non-simple parameters

メッセージ

Firefox:SyntaxError: "use strict" not allowed in function with default parameterSyntaxError: "use strict" not allowed in function with rest parameterSyntaxError: "use strict" not allowed in function with destructuring parameterChrome:SyntaxError: Illegal 'use strict' directive in function with non-simple parameter list

エラーの種類

SyntaxError

何がうまくいかなかったのか?

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

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

Function ステートメント

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

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

関数をstrict モードにしたい、かつスクリプト全体、またはエンクロージャー関数が strict モードになってもよいなら、"use strict" ディレクティブを関数の外側に移動できます:

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

Function 式

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

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

これは、次の式に変換できます:

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

アロー関数

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

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

これは、次の式に変換できます:

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

関連項目

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp