JavaScript中的严格模式
1、用法
在脚本文档的首行或者在函数的首行写上一句
'use strict'
即可。
2、严格模式的特点
有七个特点分别是:
- 参数必须使用 var 进行声明;
var num = null ; //变量必须要使用var 进行声明
- 不能够使用 with ;
var obj={
name:'kevin',
age:16,
heght:160
}
var num = 10
with(obj){
console.log('my name is' + name);
console.log('my age is' + age);
console.log('my height is' + height);
console.log(num)//10
}
//这里的with用起来还是比较方便的,但是问题在于,这种方式用易混淆
- this 不能够指向 window对象;
- 函数不能够有重名的参数;
function test(a,a,b){
return a+a+b;
}
test(1,2,3) //7
//重名的参数回复改前一个参数,所以不能够重名
- 不支持八进制数字;
var num =012; //这里是八进制的数字,才严格模式下不支持
- 不能够直接更改 arguments 参数
- 不能够使用 arguments.callee 函数指针,表示函数名;
如果上述有哪些点是不清楚的,欢迎给楼主留言。