开启严格模式:在顶部或者函数顶部加上 'use strict';
1,未经声明禁止赋值和使用
'use strict';
a = 1;
Uncaught ReferenceError: a is not defined
2,禁止使用with
with (expression) {
statement
}
expression
将给定的表达式添加到在评估语句时使用的作用域链上。表达式周围的括号是必需的。
statement
任何语句。要执行多个语句,请使用一个块语句 ({ ... })对这些语句进行分组。
var obj = {
time: 'this',
}
with(obj){
console.log(time);
}
正常模式:this
严格模式下报错:SyntaxError: Strict mode code may not include a with statement
3,eval会形成局部作用域
var a = 1;
eval('var b = 2;console.log(b);');
console.log(b);
正常模式:2
严格模式下报错:ReferenceError: b is not defined
4,禁止this关键字指向全局对象
'use strict';
function car(){
console.log(this);
}
car();
正常模式下打印Window对象
严格模式:undefined
5,禁止使用callee,caller
6,不允许对arguments赋值,argumens不在追踪变量变化
7,禁止函数重名,禁止属性名重名,禁止写只读变量