はじめに
前回に引き続き Identifier について学習ログ記事です。
Identifierの構文エラー1
arguments
/ eval
- 厳格モード(
strict mode
)かつ識別子名称がarguments
もしくはeval
(BindingIdentifier
のみ)
"use strict";
//SyntaxError Unexpected eval or arguments in strict mode
const arguments = [];
//SyntaxError Unexpected eval or arguments in strict mode
function eval() {
return '';
}
yield
- 厳格モード(
strict mode
)かつ識別子名称がyield
(IdentifierReference
/BindingIdentifier
/LabelIdentifier
すべて)
"use strict";
//SyntaxError strict mode reserved word
const yield = '';
//SyntaxError strict mode reserved word ※ReferenceErrorではない
console.log(yield);
//SyntaxError strict mode reserved word
yield: {
break yield;
}
参考:存在しない識別子へのアクセスは、通常ReferenceError
になる
//ReferenceError
console.log(obj);
-
yield
が特別な意味を持つ文脈(Generator
など)で識別子名称がyield
(BindingIdentifier
のみ)
function* generator() {
//SyntaxError Unexpected identifier 'yield'
const yield = '';
//SyntaxError Unexpected identifier 'yield' ※'-'が無い場合通常のyieldとして処理される
console.log(-yield);
//SyntaxError ※ただし、Unexpected token ':'
yield: {
break yield;
}
}
await
-
goal symbol
がModule
かつ識別子名称がawait
※Module
の文脈ではstrict mode
になる
※goal symbol
は構文解析のエントリーポイント
(IdentifierReference
/BindingIdentifier
/LabelIdentifier
すべて)
<html>
<!--head-->
<body>
<script type="module">
//SyntaxError Unexpected identifier 'yield'
const await = '';
//SyntaxError Unexpected identifier 'yield' ※'-'が無い場合通常のyieldとして処理される
console.log(await);
//SyntaxError Unexpected identifier 'yield'
await: {
break await;
}
</script>
</body>
</html>
-
awati
が特別な意味を持つ文脈(async
関数など)で識別子名称がawait
(IdentifierReference
/BindingIdentifier
/LabelIdentifier
すべて)
async function func() {
//SyntaxError Unexpected reserved word
const await = '';
//SyntaxError ※ただし、Unexpected token ')'
console.log(await);
//SyntaxError ※ただし、Unexpected token ':'
await: {
break await;
}
}
その他
- 厳格モード(
strict mode
)かつ識別子名称がimplements
/interface
/let
/package
/private
/protected
/public
/static
(IdentifierReference
/BindingIdentifier
/LabelIdentifier
すべて)
"use strict";
//SyntaxError strict mode reserved word
const implements = '';
//SyntaxError strict mode reserved word ※ReferenceErrorではない
console.log(implements);
//SyntaxError strict mode reserved word
implements: {
break implements;
}
- 識別子名称が予約語に該当する時
(IdentifierReference
/BindingIdentifier
/LabelIdentifier
すべて)
//SyntaxError Unexpected token 'break'
const break = '';
//SyntaxError Unexpected token 'case'
console.log(case);
//SyntaxError Unexpected token 'catch'
catch: {
break catch;
}
予約語についてはこちら
-
「構文エラー」という章タイトルについて
ECMA公式ドキュメントでは"Early Errors"の項に記載されているものですが、"Early Error"が定義されておらず不明瞭かつ正確な役語が見つからないこと・実際に発生するエラーのすべてが"StntaxError"だったことから「構文エラー」という章タイトルにしています。 ↩