JavaScript Use Strict

❮ 前章へ 次章へ ❯

 "use strict"; JavaScript コードを「strictモード」で実行する必要があることを定義します。


"use strict" ディレクティブ

"use strict" ディレクティブは、JavaScript 1.8.5(ECMAScriptバージョン5)の新機能です。

これはステートメントではなく、リテラル式であり、前のバージョンの JavaScript では無視されます。

"use strict" の目的は、コードを「strict モード」で実行する必要があることを示すことです。

strict モードでは、宣言していない変数を使用することはできません。

バージョン10のIE。バージョン4のFirefox。 バージョン13のChrome。バージョン5.1のSafari。 バージョン12のOpera

Strict モードは次でサポートされています:
IE は、バージョン 10 から。 Firefox は、バージョン 4 から。
Chrome は、バージョン 13 から。 Safari は、バージョン 5.1 から。
Opera は、バージョン 12 から。


Strict モードの宣言

Strict モードは、スクリプトまたは関数の先頭に "use strict"; を追加することで宣言します。

スクリプトの先頭で宣言するとグローバルスコープを持ちます(スクリプト内のすべてのコードは Strict モードで実行されます):

"use strict";
x = 3.14;       // This will cause an error because x is not declared
Try it Yourself »

"use strict";
myFunction();

function myFunction() {
    y = 3.14;   // This will also cause an error because y is not declared
}
Try it Yourself »

関数内での宣言はローカルスコープです(関数内のコードのみが strict モードです):

x = 3.14;       // This will not cause an error.
myFunction();

function myFunction() {
   "use strict";
    y = 3.14;   // This will cause an error
}
Try it Yourself »

"use strict"; の構文

strict モードを宣言するための構文は、古いバージョンの JavaScript と互換性があるように設計されています。

JavaScript プログラムでは、数値リテラル(4 + 5;)や文字列リテラル("John Doe";)をコンパイルしても副作用はありません。 単に、存在しない変数にコンパイルして終了(dies)するだけです。

従って、"use strict;" は新しいコンパイラにとって重要なのは、その意味を「理解」することだけです。 (So "use strict;" only matters to new compilers that "understand" the meaning of it.)


なぜ Strict モードか?

Strict モードは、「安全な」JavaScript の記述を容易にします。

Strict モードは、前では容認された「不正な構文」を、実際のエラーに変えます。

例えば、通常の JavaScript では、変数名を間違えて入力すると、新しいグローバル変数が作成されます。 strict モードでは、エラーをスローするので、誤ってグローバル変数を作成することはなくなります。

通常の JavaScript では、開発者は書き込み不可能なプロパティに値を代入しても、エラーのフィードバックを受け取ることはありません。

strict モードでは、書き込み不可能なプロパティ、取得専用プロパティ、存在しないプロパティ、存在しない変数、存在しないオブジェクトへ 代入しようとするとエラーがスローされます。


Strict モードで許容されないこと

変数を宣言せずに使用することはできません:

"use strict";
x = 3.14;                // This will cause an error

Try it Yourself »

オブジェクトも変数です。

オブジェクトを宣言せずに使用することはできません:

"use strict";
x = {p1:10, p2:20};      // This will cause an error

Try it Yourself »

"use strict";
var x = 3.14;
delete x;                // This will cause an error

Try it Yourself »

関数の削除はできません。

"use strict";
function x(p1, p2) {};
delete x;                // This will cause an error 

Try it Yourself »

パラメータ名が重複することはできません:

"use strict";
function x(p1, p1) {};   // This will cause an error

Try it Yourself »

8進数のリテラルは使用できません:

"use strict";
var x = 010;             // This will cause an error

Try it Yourself »

エスケープ文字は使用できません:

"use strict";
var x = \010;            // This will cause an error

Try it Yourself »

変数(またはオブジェクト)の削除はできません。

読み取り専用プロパティへの書き込みはできません:

"use strict";
var obj = {};
Object.defineProperty(obj, "x", {value:0, writable:false});

obj.x = 3.14;            // This will cause an error

Try it Yourself »

get-only プロパティへの書き込みはできません:

"use strict";
var obj = {get x() {return 0} };

obj.x = 3.14;            // This will cause an error

Try it Yourself »

削除不能なプロパティの削除はできません:

"use strict";
delete Object.prototype; // This will cause an error

Try it Yourself »

文字列 "eval" は変数として使用できません:

"use strict";
var eval = 3.14;         // This will cause an error

Try it Yourself »

文字列 "arguments" は変数として使用できません:

"use strict";
var arguments = 3.14;    // This will cause an error

Try it Yourself »

with 文は使用できません:

"use strict";
with (Math){x = cos(2)}; // This will cause an error

Try it Yourself »

セキュリティ上の理由から、eval() は呼び出されたスコープ内に変数を作成することはできません:

"use strict";
eval ("var x = 2");
alert (x);               // This will cause an error

Try it Yourself »

f() のような関数呼び出しでは、this の値はグローバルオブジェクトでした。 strict モードでは、未定義です。


将来への備え!

将来、次のような予約キーワードは strict モードでは使用できなくなります:

"use strict";
var public = 1500;      // This will cause an error

Try it Yourself »

注意!

"use strict" ディレクティブは、スクリプトや関数の先頭にないと認識されません。


❮ 前章へ 次章へ ❯