开启strict:javascript
在文件头部,或者在一个function头部内,添加‘use strict’或者“use strict”。java
Strict模式的限制,以及违反时出现的异常:git
那么这些保留字也就不能做为变量,常量、参数了。github
Uncaught SyntaxError: Unexpected strict mode reserved word
在以前的版本中。若是前缀为 0,则 JavaScript 会把数值常量解释为八进制数,若是前缀为 0 和 "x",则解释为十六进制数。正则表达式
例如:var a=0379, 会做为8进制。0x12,会做为16进制。json
可是在strict 模式下,就不容许使用8进制字面量了。浏览器
Uncaught SyntaxError: Decimals with leading zeros are not allowed in strict mode.
这一项,基本上用不到的。不须要关注。函数
之前的版本中,声明一个变量,若是不使用var修饰的话,该变量会做为一个global变量。在strict 模式下取消这一项了。测试
Uncaught ReferenceError: a is not defined
下面三种状况下不能使用eval, arguments :es5
1)不能在赋值操做符(=)的左边,
2)不能出如今后缀(++, --)操做符的左边
3)不能与一元操做符(delete, void, typeof, ++, --, +, -, ~, !)结合使用
4)不能做为函数的形参
5)还有不少其余情形
上述三种状况下,违反时会出现:
Uncaught SyntaxError: Unexpected eval or arguments in strict mode
总之一句话,就是不能乱用eval, arguments
不能使用Arguments对象的callee,不能使用Function对象的caller
Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
{a:1,b:’23’,a:123} 这样的代码是不被容许的。这个在Chrome上测试居然是能够的。
一、 Object添加了一些静态方法
1)继承相关方法:create、getPrototypeOf
2)属性相关方法:defineProperty、 defineProperties、 getOwnPropertyDescriptor、getOwnPropertyNames、 keys
3)防篡改方法:preventExtensions、isExtensible、seal、isSealed、freeze、isFrozen
须要注意的是,这些方法所有是Object对象的,不是prototype,也就是说,不是每个javascript对象都能用的。
2、Function
1)Function.prototype添加了bind()方法。
2)规范化了一个函数对象的属性caller,用于指向调用当前函数的函数的引用。
3)prototype是不可枚举的
3、Array对象
(1)判断方法:添加了静态方法Array.isArray(obj)用于判断obj是否为一个Array对象的实例。
(2)索引方法:添加了两个用于查找指定项索引的方法indexOf()和lastIndexOf()。查找时使用全等(===)进行匹配。
(3)迭代方法:添加了every()、some()、forEach()、map()、filter()方法。
(4)缩小方法:添加了reduce()和reduceRight()方法。
其中3)4)是和Java8的Stream API遥相呼应哈。
4、String对象
添加了trim()方法。
5、Date对象
添加了Date.now()、Date.prototype.toJSON()等方法。
6、RegExp对象
在ES3中,使用正则表达式字面量时共享一个RegExp实例,而在ES5中,每次使用正则表达式字面量时都要建立新的RegExp实例,就像使用RegExp构造函数同样。
7、JSON对象
添加了原生JSON内建对象。
var obj = {'a':1,'b':new Date(),'c':1}; console.log(obj); var jsonstr =JSON.stringify(obj) console.log(jsonstr) var obj1 = JSON.parse(jsonstr); console.log(obj1)
此外,各个浏览器对 ES 5的支持状况参见:http://kangax.github.io/compat-table/es5/