"use strict"
- es5.0严格模式:es3.0和es5.0产生冲突的部分,就是用es5.0,否则会用es3.0。
- "use strict";//在页面最顶端写,程序就遵循es5.0的模式了
一.不再兼容es3的一些不规则语法。使用全新的es规范。
二.两种用法:
- 全局严格模式
在页面最顶端写,程序就遵循es5.0的模式了 - 局部函数内严格模式(推荐)
eg:
function demo () {
console.log(arguments.callee);//es3, callee可以使用
}
demo();
function test() {
"use strict";//es5局部函数严格模式,callee无法使用
console.log(arguments.callee);
}
三.就是一行字符串,不会对不兼容严格模式的浏览器产生影响。
四.
- 不支持with,arguments.callee,func.caller,变量赋值前必须声明
- with:改变作用域链,节省代码(命名空间)
var obj = {
name : "obj"
}
var name = 'window';
function test () {
var name = "scope";
with(obj){//with括号中的对象是,with所圈定的代码体的作用域链的最顶端,即会去到obj中寻找name属性,再去test中寻找,再去全局GO中寻找。
console.log(name);//输出obj
}
}
document.write('a');
with(document){
write('a');
}
- 局部this必须被赋值(Person.call(null/undefined)赋值什么就是什么)
"use strict"
function test() {
console.log(this);
}
test();//输出undefined
new test();//输出test{}
test.call({});//输出Object{}
全局
"use strict";
console.log(this);//输出window
- 拒绝重复属性和参数
es3.0
function test(name,name){
console.log(name);
}
test(1,2);//输出2
es5.0中,以上情况会报错
五.eval在es3.0中都不可使用
eval('console.log(100)');//eval可以把字符串当做代码使用