JavaScript严格模式总结

由所示格式脚本进入严格模式

<script>
    "use strict";
    var a;
</script>

 

严格模式和正常模式的区别主要如下:

1、不支持八进制表示,如 var n = 023;

2、不支持with关键词

3、无法删除变量(只有configurable设置为true的对象属性,才能被删除)

1       "use strict";
2   var x;
3   delete x; // 语法错误
4   var o = Object.create(null, {'x': {
5       value: 1,
6       configurable: true
7   }});
8   delete o.x; // 删除成功    

4、不能用“eval”或“arguments”做变量或函数参数名

5、新增保留字:implements, interface, let, package, private, protected, public, static, yield。

6、Declaring function in blocks  if(a<b){ function f(){} } 

7、不可重复定义函数属性或定义重名函数

8、如果未使用new构造函数,函数中this不再指向全局对象,而是报错。

function f(){
    "use strict";
    this.a = 1;
  };
  f();// undefined

  

 参考:MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode/Transitioning_to_strict_mode

   阮一峰 http://www.ruanyifeng.com/blog/2013/01/javascript_strict_mode.html

posted @ 2015-04-29 16:20  非额  阅读(179)  评论(0编辑  收藏  举报