最近,我通过克罗福德 查了一些JavaScript代码JSLint JSLint,并给出了以下错误:
第1行第1字符1:缺少“严格使用”声明的问题。
在做一些搜索时,我意识到有些人加了"use strict";
输入 JavaScript 代码。 一旦我添加了该语句, 错误就不再出现。 不幸的是, Google 并未披露此字符串语句背后的大部分历史。 当然, 它肯定与浏览器如何解读 JavaScript 有关, 但我不知道效果会是什么 。
那么,什么是"use strict";
关于它的意义是什么,它是否仍然相关?
当前浏览器中的任意浏览器响应"use strict";
字符串, 还是用于未来用途 ?
如果人们担心使用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.
*/