【问题标题】:JavaScript: Can ECMAScript 5's Strict Mode ("use strict") be enabled using single quotes ('use strict')?JavaScript:能否使用单引号('use strict')启用 ECMAScript 5 的严格模式(“use strict”)?
【发布时间】:2011-07-10 00:30:02
【问题描述】:

JavaScript 不关心您的字符串是双引号 "double" 还是单引号 'single'

ECMAScript 5 严格模式的每个示例都使用双引号中的"use strict" 启用它。我可以执行以下操作(单引号):

alert(function(){
  'use strict';
  return !this;
}());

如果启用了严格模式,这将返回 true,如果没有启用,则返回 false

【问题讨论】:

标签: javascript ecma262 strict ecmascript-5


【解决方案1】:

For you, without using a browser that supports strict mode:

使用严格指令是指令序言中的ExpressionStatement,其StringLiteral 是精确的字符序列"use strict"'use strict'。使用严格指令可能不包含 EscapeSequence 或 LineContinuation。

【讨论】:

  • 如果您使用 gzip 压缩内容,那么我建议您使用双引号或单引号,具体取决于文档中哪个出现最多。
  • 好的,但是JS引擎支持怎么样?我认为目前没有任何已知的怪癖,但我不确定。
  • @Pumbaa80:你的意思是如果不支持严格模式有问题吗?我对此表示高度怀疑。选择语法是为了使其向后兼容。不支持严格模式的引擎只会看到带有单独字符串文字的表达式语句。
  • @FelixKling 我不是这个意思。我想知道是否有一个引擎在看到"use strict" 时会触发严格模式,但在看到'use strict' 时不会触发严格模式,即使两者应该是等价的。
  • @Pumbaa80:啊。我不知道,我不这么认为。
【解决方案2】:

http://ecma262-5.com/ELS5_HTML.htm#Section_14.1

Use Strict Directive 是 Directive Prologue 中的 ExpressionStatement,其 StringLiteral 是确切的字符序列 "use strict"'use strict'。使用严格指令可能不包含 EscapeSequence 或 LineContinuation。

【讨论】:

    【解决方案3】:

    根据the mozilla documentation,您可以同时使用"use strict";'use strict';

    【讨论】:

      猜你喜欢
      • 2014-01-17
      • 2012-02-15
      • 2019-10-16
      • 2014-02-17
      • 1970-01-01
      • 2011-10-23
      • 2020-12-07
      • 2015-10-19
      相关资源
      最近更新 更多