不再兼容es3的一些不规则语法,使用全新的es5规范
就是一行字符串,不会对不兼容严格模式的浏览器产生影响
写在页面逻辑最顶端:
"use strict" function test(){ //代码块 } test();写在局部函数的最顶端:
function test(){ "use strict" //代码块 } test();不支持with,arguments.callee,caller、变量赋值前必须声明、局部this必须被赋值、拒绝重复属性和参数
以下列举使用不支持部分的例子,在严格模式下会报错,可以分别尝试。
es3.0支持,但es5.0禁用的一个函数 函数功能:此函数能通过参数改变直接作用域。例如,不同部门的人写同样的属性名,利用with方法不会变量污染
"use strict" var org = { dp1:{ a:{ name:"a" }, b:{ name:"b" } }, dp2:{ } } with(org.dp1.a){ console.log(name); //a } with(org.dp1.b){ console.log(name); //b } //报错 Uncaught SyntaxError: //Strict mode code may not include a with statement函数功能:返回所在函数的引用
function test(){ "use strict" console.log(arguments.callee) } test(); //报错VM100:3 Uncaught TypeError: 'caller', 'callee', and 'arguments' //properties may not be accessed on strict mode functions or //the arguments objects for calls to them函数功能:返回函数的调用环境
"use strict" function test(){ } function test(){ demo(); } function demo(){ console.log(demo.caller) } test(); //报错VM100:3 Uncaught TypeError: 'caller', 'callee', and 'arguments' //properties may not be accessed on strict mode functions or //the arguments objects for calls to them当前浏览器基于es3.0+es5.0新增方法,即
对于es3.0和es5.0产生冲突的地方,开启es5.0严格模式的时候,他们冲突的部分就用es5.0,否则就用es3.0