gpt4 book ai didi

javascript - 当我们使用模板文字时 `use strict` 不工作

转载 作者:行者123 更新时间:2023-12-05 09:06:17 24 4
gpt4 key购买 nike

如果我用反引号/模板文字将 use strict 括起来,'use strict' 不会按预期工作。你能分享一下背后的原因吗?是否存在模板文字无法按预期工作的类似异常语句?

`use strict`;
x = 3.14; // Ideally it should cause an error (as x is not defined).
alert(x);

`use strict`; // if we enclose in single quotes or double quotes
x = 3.14; // Ideally it should cause an error (as x is not defined).
alert(x);

最佳答案

它就是这样设计的。

来自ES6 specification :

14.1.1 Directive Prologues and the Use Strict Directive

A Directive Prologue is the longest sequence of ExpressionStatement productions occurring as the initial StatementListItem or ModuleItem productions of a FunctionBody, a ScriptBody, or a ModuleBody and where each ExpressionStatement in the sequence consists entirely of a StringLiteral token followed by a semicolon. The semicolon may appear explicitly or may be inserted by automatic semicolon insertion. A Directive Prologue may be an empty sequence.

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.

A Directive Prologue may contain more than one Use Strict Directive. However, an implementation may issue a warning if this occurs.

强调我的。

它明确指出,为了使 Use Strict 指令起作用,它必须用单引号或双引号引起来,但模板文字根本不允许用于此目的。

关于javascript - 当我们使用模板文字时 `use strict` 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66175819/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com