Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
415 views
in Technique[技术] by (71.8m points)

javascript - “严格使用”在JavaScript中有什么作用,其背后的原因是什么?(What does “use strict” do in JavaScript, and what is the reasoning behind it?)

Recently, I ran some of my JavaScript code through Crockford's JSLint , and it gave the following error:

(最近,我通过Crockford的JSLint运行了一些JavaScript代码,并给出了以下错误:)

Problem at line 1 character 1: Missing "use strict" statement.

(第1行第1个字符处的问题:缺少“使用严格”语句。)

Doing some searching, I realized that some people add "use strict";

(进行搜索后,我发现有些人添加了"use strict";)

into their JavaScript code.

(纳入他们的JavaScript代码。)

Once I added the statement, the error stopped appearing.

(添加语句后,错误停止出现。)

Unfortunately, Google did not reveal much of the history behind this string statement.

(不幸的是,谷歌没有透露此字符串语句背后的许多历史。)

Certainly it must have something to do with how the JavaScript is interpreted by the browser, but I have no idea what the effect would be.

(当然,它一定与浏览器如何解释JavaScript有关,但是我不知道会有什么影响。)

So what is "use strict";

(那么什么是"use strict";)

all about, what does it imply, and is it still relevant?

(所有的一切,这意味着什么,并且仍然有意义?)

Do any of the current browsers respond to the "use strict";

(当前的浏览器是否响应"use strict";)

string or is it for future use?

(字符串还是将来使用?)

  ask by Mark Rogers translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This article about Javascript Strict Mode might interest you: John Resig - ECMAScript 5 Strict Mode, JSON, and More

(这篇有关Javascript严格模式的文章可能会让您感兴趣: John Resig-ECMAScript 5严格模式,JSON等)

To quote some interesting parts:

(引用一些有趣的部分:)

Strict Mode is a new feature in ECMAScript 5 that allows you to place a program, or a function, in a "strict" operating context.

(严格模式是ECMAScript 5中的一项新功能,可让您将程序或功能置于“严格”的操作环境中。)

This strict context prevents certain actions from being taken and throws more exceptions.

(这种严格的上下文会阻止采取某些措施,并引发更多异常。)

And:

(和:)

Strict mode helps out in a couple ways:

(严格模式可以通过以下两种方式提供帮助:)

  • It catches some common coding bloopers, throwing exceptions.

    (它捕获了一些常见的编码漏洞,并引发异常。)

  • It prevents, or throws errors, when relatively "unsafe" actions are taken (such as gaining access to the global object).

    (当采取相对“不安全”的操作(例如获得对全局对象的访问权限)时,它可以防止或引发错误。)

  • It disables features that are confusing or poorly thought out.

    (它禁用令人困惑或考虑不周的功能。)

Also note you can apply "strict mode" to the whole file... Or you can use it only for a specific function (still quoting from John Resig's article) :

(还要注意,您可以将“严格模式”应用于整个文件...或者您只能将其用于特定功能(仍引用John Resig的文章) :)

 // Non-strict code... (function(){ "use strict"; // Define your library strictly... })(); // Non-strict code... 

Which might be helpful if you have to mix old and new code ;-)

(如果您必须混合使用新旧代码,这可能会有所帮助;-))

So, I suppose it's a bit like the "use strict" you can use in Perl (hence the name?) : it helps you make fewer errors, by detecting more things that could lead to breakages.

(因此,我想这有点像您可以在Perl中使用"use strict" (因此得名?) :它通过检测更多可能导致损坏的内容来帮助您减少错误。)

Strict mode is now supported by all major browsers .

(所有主要浏览器现在都支持严格模式。)

Inside native ECMAScript modules (with import and export statements) and ES6 classes , strict mode is always enabled and cannot be disabled.

(在本机ECMAScript模块 (带有importexport语句)和ES6类中 ,严格模式始终处于启用状态,不能被禁用。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...