这会启用“use strict”吗?全球?

发布于 2024-11-16 23:56:06 字数 705 浏览 2 评论 0原文

类似,但不一样,如何全局启用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤者何惧 2024-11-23 23:56:06

TL;DR

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

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


更新

现在在规范中更清楚了(也许在 ES5 中很清楚,但对我来说不是),是的,单独的 script 元素出于“使用的目的而分开严格”。原始答案中的引用有 稍作更改,表示“源文本”而不​​是“代码单元”,并且 脚本和模块部分将进行更详细的介绍。


原始答案

规范说:

由于严格模式是在语法代码单元级别选择的,因此严格模式仅施加在此类代码单元内具有局部效果的限制。严格模式不会限制或修改必须跨多个代码单元一致运行的 ECMAScript 语义的任何方面。

(第 4.2.2 节)

所以问题是:不同的 script 标签是否有不同的语法代码单元?

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

假设没有显示 foo 的声明,则此代码将成为 正常模式下隐式全局变量的恐怖

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

在正常模式下,会创建一个值为“bar”的新全局变量 foo 并显示“foo = bar” 消息。在严格模式下,由于 foo 未定义,因此会引发异常。

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

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

...我会按预期得到异常(实时示例) 。但是,如果我将它们放在单独的 script 标记中:

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

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

同样,如果该函数在另一个文件中关闭并且我这样做:

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

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

请注意,此处的示例标记:

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

无效。 script 标记可以具有 src 属性内容,但不能同时具有两者。 (嗯,基本上;详细信息此处 [HTML5 ] 和此处 [HTML 4.01]。)如果有一个src 元素,浏览器应该忽略内容,而且大多数浏览器都会忽略。最多。 :-)

TL;DR:

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

(Separately, re the script tag at the end of the question: If a script element has a src, 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:

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.

(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:

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

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 because foo is undefined.

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 script tags, though:

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

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:

<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 script tag may either have a src attribute or content, but not both. (Well, basically; details here [HTML5] and here [HTML 4.01].) If it has a src element, the browser is supposed to disregard the content, and most do. Most. :-)

初心未许 2024-11-23 23:56:06

JSLint 突然报告:使用函数形式“使用严格”

(function () {
    "use strict";
    // put all of your strict code here


}());

JSLint is suddenly reporting: Use the function form of "use strict"

(function () {
    "use strict";
    // put all of your strict code here


}());
梦醒时光 2024-11-23 23:56:06

不,脚本标签被视为程序,因此是代码单元“use strict” 不应从一个脚本标记转移到另一个脚本标记。

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

no, script tags are considered programs and are therefor code 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 the program/script tag as it has no pointer/reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文