什么是 “use strict”? 使用它的好处和坏处是什么?

严格模式是ES5引入的,更好的将错误检测引入代码的方法。顾名思义,使得JS在更严格的条件下运行。

变量必须先声明,再使用
function test(){
  "use strict";
  foo = 'bar';  // Error
}
 
不能对变量执行delete操作
var foo = "test";
function test(){}
 
delete foo; // Error
delete test; // Error
 
function test2(arg) {
    delete arg; // Error
}
对象的属性名不能重复
{ foo: true, foo: false } // Error
 
禁用eval()
 
函数的arguments参数
setTimeout(function later(){
  // do stuff...
  setTimeout( later, 1000 );
}, 1000 );
 
禁用with(){}
 
不能修改arguments
不能在函数内定义arguments变量
不能使用arugment.caller和argument.callee。因此如果你要引用匿名函数,需要对匿名函数命名。
posted @ 2017-05-29 20:56  少东主  阅读(2817)  评论(0编辑  收藏  举报