最近,我通过克罗福德 查了一些JavaScript代码JSLint JSLint,并给出了以下错误:
第1行第1字符1:缺少“严格使用”声明的问题。
在做一些搜索时,我意识到有些人加了"use strict";
输入 JavaScript 代码。 一旦我添加了该语句, 错误就不再出现。 不幸的是, Google 并未披露此字符串语句背后的大部分历史。 当然, 它肯定与浏览器如何解读 JavaScript 有关, 但我不知道效果会是什么 。
那么,什么是"use strict";
关于它的意义是什么,它是否仍然相关?
当前浏览器中的任意浏览器响应"use strict";
字符串, 还是用于未来用途 ?
严格模式允许在 v8 引擎中设置严格的特性。 某些特性的短示例 :
您可通过写作方式在全球范围启用它 :
'use strict'; // strict mode enabled!
您只是在函数中包含的每个函数 :
let myfunc = () => {
'use strict'; // strict mode enabled
b = 0; // broke
}
- 在使用该变量( sane imo) 之前, 您必须声明变量 :
var x;
x = '0'; // ok
y = ''; // not ok
还有更多的特征,检查在这里更多! 更多! 更多! 更多! 更多!
如果人们担心使用use strict
也许值得看看这篇文章:
在浏览器中支持 ECMAScript 5 “ 立体模式” 。 这是什么意思 ?
NovoGeeek.com - 克里希纳的博客
讨论浏览器支持, 但更重要的是如何安全处理:
function isStrictMode(){
return !this;
}
/*
returns false, since 'this' refers to global object and
'!this' becomes false
*/
function isStrictMode(){
"use strict";
return !this;
}
/*
returns true, since in strict mode the keyword 'this'
does not refer to global object, unlike traditional JS.
So here, 'this' is 'undefined' and '!this' becomes true.
*/