「JavaScript/strictモード」の版間の差分
削除された内容 追加された内容
タグ: 2017年版ソースエディター |
|||
1 行
{{Nav}}
'''Strict モード'''は、通常はエラーとはせず
Strict モードは、ECMAScript 2009 = ES5 で導入されました<ref>初見: https://www.ecma-international.org/wp-content/uploads/ECMA-262_5th_edition_december_2009.pdf#page=61 10.1.1 Strict Mode Code、現行: https://262.ecma-international.org/#sec-strict-mode-of-ecmascript C The Strict Mode of ECMAScript</ref>。
7 行
'''スクリプト全体をStrict モードにする場合'''
<
'use strict';
var msg = "Hello strict world!";
console.log(msg);
</syntaxhighlight>
この例では怪しい兆候がないので、Hello strict world! が表示されました。
<
'use strict';
msg = "Hello strict world!";
console.log(msg);
</syntaxhighlight>
1箇所変更しました。
実行すると、'''ReferenceError: msg is not defined''' となります。
24 行
{{code|'use strict';}} をとってみます。
<
msg = "Hello strict world!";
console.log(msg);
</syntaxhighlight>
Hello strict world! が表示されました。
'''関数単位でStrict モードにする場合'''
<
console.log(0)
function nostrict() {
45 行
console.log(2)
// SyntaxError: Octal literals are not allowed in strict mode.
</syntaxhighlight>
console.log() による表示は全く行われないでエラーで止まります。
0に[0-7]が続く書式の八進数表現はstrictモードではエラーです。
== class 定義は自動的に strict モード ==
ES6で導入された[[JavaScript/クラス|クラス]](class)の定義の中では、自動的にstrict モードになります。
== strict モードのエラー対象 ==
|