严格模式
ES5引入了严格模式的概念。在严格模式下,会对某些不安全的操作抛出错误。
全局严格模式
要在整个js文件中启用严格模式,只需要在js文件的最顶部添加下面一行代码即可:
"use strict";
局部严格模式
如果在函数内部最顶部添加了’use strict’;则在该函数内部启用了严格模式
function useStrict(){
"use strict";
// do something
}
严格模式对编写代码的影响
常见的影响有以下几个:
- 在严格模式下,变量的声明必须使用var开始,不能省略,在正常模式下,可以省略,省略即为全局变量。
// 严格模式下
"use strict";
a = 10; // 报错:Uncaught ReferenceError: a is not defined
console.log(a);
// 正常模式下
a = 10;
console.log(a); // 10
- 在严格模式下,不能定义名为eval或arguments的变量,否则会导致语法错误
// 严格模式
"use strict";
var eval = 5; // 报错:Uncaught SyntaxError: Unexpected eval or arguments in strict mode
// 正常模式
var eval = 5;
console.log(eval); // 5
- 在严格模式下,不能使用with语句
"use strict";
var a = 10;
with(a){
a = 20; // 报错:Uncaught SyntaxError: Strict mode code may not include a with statement
}
- 在严格模式下,使用eval可以创建第三种作用域
"use strict";
var a = 10;
console.log(eval("var a = 20;a")); // 20
console.log(a); // 10
- 禁止this指向全局对象window
function fn(){
return !this; // false => 此时this指向的是window对象
}
function fn1(){
"use strict";
return !this; // true => 此时this为undefined,所以取反为true.
}
所以在构造函数中,如果在创建对象的实例的时候,忘记了new关键字,那么在严格模式下就会报错
function F(){
"use strict";
this.a = 1;
}
var aa = F(); // 报错:Uncaught TypeError: Cannot set property 'a' of undefined
- 在严格模式下,函数的arguments属性和caller属性不能使用
function f1(){
"use strict";
f1.caller; // 报错: Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
f1.arguments;
}
f1();
- 在严格模式下,删除configurable配置为false的变量,会报错
"use strict";
var a = 1;
delete a; // 报错:Uncaught SyntaxError: Delete of an unqualified identifier in strict mode.
- 在严格模式下,对一个对象的只读属性赋值会报错,正常模式下会失败。
"use strict";
var o = {};
Object.defineProperty(o,'x',{
value:1,
writeable:false
});
o.x = 2; // 报错:Uncaught TypeError: Cannot assign to read only property 'x' of object '#<Object>'
关于更多的严格模式的限制,请参考以下这篇文章:http://www.ruanyifeng.com/blog/2013/01/javascript_strict_mode.html