【发布时间】:2021-05-16 10:31:25
【问题描述】:
如果我用反引号/模板文字将 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);
【问题讨论】:
-
use string只是不适用于模板文字 -
我更喜欢模板文字,所以尝试过,它是否会在这个特殊的字符串上工作......
-
当它只是一个简单的字符串时,为什么要使用模板文字? “要为整个脚本调用严格模式,请将确切的语句
"use strict";(或'use strict';)放在任何其他语句之前。” Source -
存在模板文字并不意味着您需要用一个替换每个字符串文字......
-
您希望
`use ${something}`;为变量something工作吗?
标签: javascript template-literals use-strict