JavaScript: Can ECMAScript 5's Strict Mode (“use strict”) be enabled using single quotes ('use strict')?

落爺英雄遲暮 提交于 2019-12-09 14:00:04

问题


JavaScript doesn't care if your Strings are double-quoted "double" or single-quoted 'single'.

Every example of ECMAScript 5's strict mode has it enabled by "use strict" in double-quotes. Can I do the following (single-quotes):

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

This will return true if Strict mode is enabled, and false if it is not.


回答1:


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

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.




回答2:


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

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.




回答3:


According to the mozilla documentation you can use both "use strict"; and 'use strict';.



来源:https://stackoverflow.com/questions/5214391/javascript-can-ecmascript-5s-strict-mode-use-strict-be-enabled-using-singl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!