javascript 这会在全球范围内启用“严格使用”吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6483768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:50:26  来源:igfitidea点击:

Would this enable "use strict" globally?

javascriptstrict

提问by graham.reeds

Similar, but not the same as, How to enable ECMAScript "use strict" globally?

类似但不一样,如何在全局启用 ECMAScript “使用严格”?

I have bought JavaScript Patternsand 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:

我已经购买了JavaScript Patterns,它建议启用 use strict。将它添加到两打 javascript 文件中会有点麻烦,所以全局启用它会很好。我最初想像这样添加到我的 main.js 的顶部:

"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?

这些中的任何一个都会全局启用 ECMAScript 5 严格模式吗?

回答by T.J. Crowder

TL;DR:

特尔;博士

No, a "use strict"in one scriptelement does not impose "use strict"on code in other scriptelements. It applies only to the source text it's part of.

不,"use strict"一个script元素中的 a 不会强加于"use strict"其他script元素中的代码。它仅适用于它所属的源文本。

(Separately, re the scripttag at the end of the question: If a scriptelement has a src, any inline text it has is considered "documentation" and is ignored.)

(另外,重新script标记问题末尾的标签:如果script元素具有src,则它具有的任何内联文本都被视为“文档”并被忽略。)



Update:

更新

It's clearer in the specification now (maybe it was clear in ES5, but just not to me) that yes, separate scriptelements are separate for the purposes of "use strict". The quote below in the original answer has been changed slightlyto say "source text" rather than "code unit", and the Scripts and Modulessection goes into more detail.

现在在规范中更清楚(也许在 ES5 中很清楚,但对我来说不是)是的,script为了"use strict". 原始答案中的以下引用略有更改,改为“源文本”而不​​是“代码单元”,脚本和模块部分更详细。



Original answer:

原答案

The specificationsays:

规范说:

Because strict mode is selected at the level of a syntactic code unit, strict mode only imposes restrictions that have local effect within such a code unit. Strict mode does not restrict or modify any aspect of the ECMAScript semantics that must operate consistently across multiple code units.

因为严格模式是在语法代码单元级别选择的,所以严格模式只施加在这样的代码单元内具有局部影响的限制。严格模式不会限制或修改必须跨多个代码单元一致运行的 ECMAScript 语义的任何方面。

(Section 4.2.2)

(第 4.2.2 节)

So the question is: Are different scripttags different syntactic code units?

所以问题是:不同的script标签是不同的句法代码单元吗?

V8 (the JavaScript engine inside Chrome) appears to believe that they areseparate 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.

V8(Chrome 中的 JavaScript 引擎)似乎认为它们独立的,因此"use strict";在页面顶部的全局范围内放置一个是行不通的。也许它在我还没有找到的地方被指定,但无论如何,这是一个合理的解释。

Assuming no declaration for foothat isn't shown, this code falls prey to The Horror of Implicit Globalsin normal mode:

假设没有foo显示任何声明,此代码在正常模式下成为隐式全局的恐怖的牺牲品:

function test() {
    try {
      foo = "bar";
      display("foo = " + foo);
    }
    catch (e) {
      display("Exception: " + e);
    }
}

In normal mode, that creates a new global variable foowith the value "bar" and shows the "foo = bar"message. In strict mode, an exception is thrown because foois undefined.

在正常模式下,这会创建一个foo值为“bar”的新全局变量并显示"foo = bar"消息。在严格模式下,会因为foo未定义而抛出异常。

If I put this script tag in a page:

如果我将此脚本标记放在页面中:

<script>
"use strict";
function test() {
    try {
      foo = "bar";
      display("foo = " + foo);
    }
    catch (e) {
      display("Exception: " + e);
    }
}
</script>

...I get the exception as expected (live example). If I put them in separate scripttags, though:

...我得到了预期的异常(现场示例)。但是,如果我将它们放在单独的script标签中:

<script>
"use strict";
</script>
<script>
function test() {
    try {
      foo = "bar";
      display("foo = " + foo);
    }
    catch (e) {
      display("Exception: " + e);
    }
}
</script>

I don'tget the exception (on V8) (example). And that's reasonable if you think about how the browser and the JavaScript engine are interacting.

没有得到异常(在 V8 上)(示例)。如果您考虑浏览器和 JavaScript 引擎是如何交互的,那么这是合理的。

And similarly, if the function is off in another file and I do this:

同样,如果该功能在另一个文件中关闭,我会这样做:

<script>
"use strict";
</script>
<script src="/inatoq"></script>

I don't get the exception (example), presumably for the same reason.

我没有得到例外(例如),大概是出于同样的原因。

Note that your example tag here:

请注意您的示例标签:

<script data-main="lib/main" src="lib/require.js">"use strict"</script>

is invalid. A scripttag may eitherhave a srcattribute orcontent, but not both. (Well, basically; details here[HTML5] and here[HTML 4.01].) If it has a srcelement, the browser is supposed to disregard the content, and most do. Most. :-)

是无效的。一个script标签可能要么有一个src属性内容,但不能同时使用。(嗯,基本上;这里[HTML5] 和这里[HTML 4.01] 的详细信息。)如果它有一个src元素,浏览器应该忽略内容,而且大多数都这样做。最多。:-)

回答by Jeremy Bell

回答by BGerrissen

no, script tags are considered programsand are therefor code units. "use strict"should not carry over from one script tag to another.

不,脚本标签被考虑programs,因此code units"use strict"不应从一个脚本标签转移到另一个脚本标签。

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 the program/script tag as it has no pointer/reference.

每个脚本标签都被单独解释并且实际上有它们自己的作用域。这个范围并不明显,因为全局声明的所有内容都将在全局对象上结束,但它仍然存在。该字符串"use strict"将在program/script 标记的末尾被垃圾收集,因为它没有指针/引用。