这会启用“use strict”吗?全球?
类似,但不一样,如何全局启用 ECMAScript“use strict”?
我买了JavaScript 模式,它建议启用 use strict。将其添加到两打 javascript 文件中会有点麻烦,因此全局启用它会很好。我最初考虑像这样添加到我的 main.js 的顶部:
"use strict"
require({
priority: ["jquery", "raphael", "myapp"]
});
但是我后来认为它可能只会为该文件启用它。然后我想到了这一点:
<script data-main="lib/main" src="lib/require.js">"use strict"</script>
其中任何一个都会在全局范围内启用 ECMAScript 5 严格模式吗?
Similar, but not the same as, How to enable ECMAScript "use strict" globally?
I have bought JavaScript Patterns and it recommends enabling use strict. Adding it to the two dozen javascript files would be a bit of a chore, so enabling it globally would be nice. I originally thought about adding to the top of my main.js like this:
"use strict"
require({
priority: ["jquery", "raphael", "myapp"]
});
However I then thought that it maybe would only enable it for that file. I then thought about this:
<script data-main="lib/main" src="lib/require.js">"use strict"</script>
Would either of these enable ECMAScript 5 strict mode globally?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
TL;DR:
否,一个
script
元素中的“use strict”
不会强加“use strict”
其他script
元素中的代码。它仅适用于它所属的源文本。(另外,重新考虑问题末尾的
script
标记:如果script
元素具有src
,则考虑它具有的任何内联文本“文档”并且被忽略。)更新:
现在在规范中更清楚了(也许在 ES5 中很清楚,但对我来说不是),是的,单独的
script
元素出于“使用的目的而分开严格”。原始答案中的引用有 稍作更改,表示“源文本”而不是“代码单元”,并且 脚本和模块部分将进行更详细的介绍。
原始答案:
规范说:
(第 4.2.2 节)
所以问题是:不同的
script
标签是否有不同的语法代码单元?V8(Chrome 中的 JavaScript 引擎)似乎认为它们是独立的,因此在页面顶部的全局范围内放置一个
"use strict";
不会工作。也许它在我还没有找到的地方指定,但无论如何,这是一个合理的解释。假设没有显示
foo
的声明,则此代码将成为 正常模式下隐式全局变量的恐怖:在正常模式下,会创建一个值为“bar”的新全局变量
foo
并显示“foo = bar”
消息。在严格模式下,由于 foo 未定义,因此会引发异常。如果我将此脚本标记放入页面中:
...我会按预期得到异常(实时示例) 。但是,如果我将它们放在单独的
script
标记中:我不会得到异常(在 V8 上)(示例)。如果您考虑一下浏览器和 JavaScript 引擎如何交互,这是合理的。
同样,如果该函数在另一个文件中关闭并且我这样做:
我没有得到异常(示例),大概是出于同样的原因。
请注意,此处的示例标记:
无效。
script
标记可以具有src
属性或内容,但不能同时具有两者。 (嗯,基本上;详细信息此处 [HTML5 ] 和此处 [HTML 4.01]。)如果有一个src
元素,浏览器应该忽略内容,而且大多数浏览器都会忽略。最多。 :-)TL;DR:
No, a
"use strict"
in onescript
element does not impose"use strict"
on code in otherscript
elements. It applies only to the source text it's part of.(Separately, re the
script
tag at the end of the question: If ascript
element has asrc
, any inline text it has is considered "documentation" and is ignored.)Update:
It's clearer in the specification now (maybe it was clear in ES5, but just not to me) that yes, separate
script
elements are separate for the purposes of"use strict"
. The quote below in the original answer has been changed slightly to say "source text" rather than "code unit", and the Scripts and Modules section goes into more detail.Original answer:
The specification says:
(Section 4.2.2)
So the question is: Are different
script
tags different syntactic code units?V8 (the JavaScript engine inside Chrome) appears to believe that they are separate and so putting a single
"use strict";
in global scope at the top of your page would not work. Perhaps it's specified somewhere I haven't found yet, but in any case, it's a reasonable interpretation.Assuming no declaration for
foo
that isn't shown, this code falls prey to The Horror of Implicit Globals in normal mode:In normal mode, that creates a new global variable
foo
with the value "bar" and shows the"foo = bar"
message. In strict mode, an exception is thrown becausefoo
is undefined.If I put this script tag in a page:
...I get the exception as expected (live example). If I put them in separate
script
tags, though:I don't get the exception (on V8) (example). And that's reasonable if you think about how the browser and the JavaScript engine are interacting.
And similarly, if the function is off in another file and I do this:
I don't get the exception (example), presumably for the same reason.
Note that your example tag here:
is invalid. A
script
tag may either have asrc
attribute or content, but not both. (Well, basically; details here [HTML5] and here [HTML 4.01].) If it has asrc
element, the browser is supposed to disregard the content, and most do. Most. :-)JSLint 突然报告:使用函数形式“使用严格”
JSLint is suddenly reporting: Use the function form of "use strict"
不,脚本标签被视为
程序
,因此是代码单元
。“use strict”
不应从一个脚本标记转移到另一个脚本标记。每个脚本标签都是单独解释的,并且实际上有自己的范围。这个范围并不明显,因为全局声明的所有内容都将最终出现在全局对象上,但它仍然存在。字符串“use strict”将在
program
/script 标记末尾被垃圾回收,因为它没有指针/引用。no, script tags are considered
programs
and are thereforcode units
."use strict"
should not carry over from one script tag to another.Each script tag is interpreted individually and actually have their own scope. This scope is not noticable since everything declared globally will end up on the global object, but it's there nontheless. The string
"use strict"
will be garbage collected at the end of theprogram
/script tag as it has no pointer/reference.