ES5严格模式(strict mode)
顾名思义,这种模式使得Javascript在更严格的条件下运行。
-消除Javascript语法的一些不合理、不严谨之处,减少一些怪异行为;
-消除代码运行的一些不安全之处,保证代码运行的安全;
-提高编译器效率,增加运行速度;
-为未来新版本的Javascript做好铺垫。
进入严格模式 “use strict”
"use strict"
console.log("已经进入严格模式");
严格模式行为变更:
1.全局变量声明时 必须加var
"use strict"
a = 10;//报错 因为 a没有被var 声明
//Uncaught ReferenceError: a is not defined; 引用错误: a 没有被声明
2.this 无法指向全局对象
"use strict"
// console.log("已经进入严格模式");
function a(){
this.b = 10; //报错 , 因为this指向了window对象;
//Uncaught TypeError: Cannot set property 'b' of undefined;
//类型错误 : 不能给undefined设置属性b;
}
window.a();
3.函数内重名属性
"use strict";
function a(b,b,c){
//报错
// Uncaught SyntaxError: Duplicate parameter name not allowed in this context;语法错误:在此上下文中不允许重复的参数名称
}
4.arguments对象;
4.1 arguments对象不允许被动态改变;
function fn1(a) {
a = 2;
return [a, arguments[0]];
}
console.log(fn1(1