最近,我通过Crockford的jslint 输入了一些我的笔记本代码, 它犯了以下错误:
第1行字符1:缺少“严格使用”的语句。
某些搜索, 我意识到有些人在他们的 javascript 代码中添加了“ 严格使用 ” ; 。 一旦我添加了该语句, 错误就不再出现。 不幸的是, Google 并没有揭示此字符串语句背后的大部分历史。 当然, 它肯定与浏览器如何解读该语句有关, 但我不知道效果会是什么 。
那么,什么是“严格使用”;什么是“严格使用”;一切,它意味着什么,它是否仍然相关?
当前的任何浏览器是否响应“ 严格使用 ” ; 字符串, 或是否未来使用 ?
语句“ 严格使用 ” ; 指示浏览器使用严格模式, 即一个缩写和安全的 Javastrict 特征集 。
特征列表列表(并非详尽无遗)
不允许的全变量 。 (在变量名称中缺少 var 声明和打字符) 静态失效分配将会在严格模式下丢出错误( 指派 nan = 5 ;) 试图删除非可删除属性将会丢弃( 删除对象 . prototype) , 要求对象字典中的所有属性名称都是独特的( var x = {x1: 1, x1: 1, x1: 2}) 函数参数名称必须是独一无二的( 函数和( x, x) {...}) 禁止八语法( var x = 023; 一些 deds 错误地假定前的零 doe
[参考:严格模式,mozilla 开发者网络]
实例:
严格模式代码不能用别名来描述在其中创建的参数对象的属性
function show( msg ){
msg = 42;
console.log( msg ); // msg === 42
console.log( arguments[0] ); // arguments === 42
}
show( "Hey" );
// In strict mode arguments[i] does not track the value of
// the corresponding named argument, nor does a named argument track the value in the corresponding arguments[i]
function showStrict( msg ){
"use strict";
msg = 42;
console.log( msg ); // msg === 42
console.log( arguments[0] ); // arguments === "Hey"
}
showStrict( "Hey" );
要比较的小示例 :
无限制模式 :
(一) [1,2,3] 控制台.log(一) / 输出: / 1/ / 2/ 3
严格模式 :
“ 严格使用 ” ; 对于 (i) [1, 2, 3]) 控制台. log(i) / 输出: // / 未捕获引用器: i 未定义
无限制模式 :
字符串. prototype. test = 函数 () { 控制台. log( 此“ 字符串” 类型); } } ; a. test (); // 输出 // 假
字符串类型. prototype. test = 函数 () {“ 严格使用 ” {; 控制台. log( 此“ 字符串” 的类型) } ; a. test (); // 输出 / / true
如果人们担心使用严格使用,
浏览器中的 5 个“ 限制模式” 支持 。 这是什么意思 ? novogeek. com - krishna 的 Webb 。
讨论浏览器支持, 但更重要的是如何安全处理:
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.
*/