#javascript #ecmascript_6
Если написать use strict в косых апострофах, то функция не переходит в строгий режим.
Верно ли это поведение?
function f() { // Нет строгого режима - всё хорошо
return 010;
}
console.log(f());
function f() { // Почему тут нет строгого режима?
`use strict`;
return 010;
}
console.log(f());
function f() { // Строгий режим, поэтому ошибка
'use strict';
return 010;
}
console.log(f());
function f() { // Строгий режим, поэтому ошибка
"use strict";
return 010;
}
console.log(f());
Ответы
Ответ 1
В соответствии с текущим стандартом это верно. Из стандарта Директивы и "use strict" A Use Strict Directive is an ExpressionStatement in a Directive Prologue whose StringLiteral is either the exact code unit sequences "use strict" or 'use strict'. A Use Strict Directive may not contain an EscapeSequence or LineContinuation. StringLiteral определена как StringLiteral:: " DoubleStringCharactersopt " ' SingleStringCharactersopt ' То что в косых апострофах называется Template Literal Lexical Components. К StringLiteral это не относится.Ответ 2
А так в документации написано: A Use Strict Directive is an ExpressionStatement in a Directive Prologue whose StringLiteral is either the exact character sequences "use strict" or 'use strict'. A Use Strict Directive may not contain an EscapeSequence or LineContinuation. P.S. Вопрос на инглише https://stackoverflow.com/q/5214391/5441700
Комментариев нет:
Отправить комментарий