ECMAScript 5 中的新增特性
我在网上看到了 这样一篇 介绍 ES5 中新增特性的文章,简单翻译下,分享给大家。
现在 ES6 在所有现代浏览器中得到了很好的支持,ES5 更是,兼容性达到了 IE9+(IE9 除严格模式不支持外,其他 ES5 特性全部支持)。
JavaScript 是全面向前兼容的,所以学习 ES5 并不是没用。即使现在有了像 Babel 这样的转码工具,但是对语言本身足够了解的话,更能帮助我们原生写出兼容性更好的代码。
- 严格模式
'use strict';
- 对象的访问器属性
var obj = {
get foo() {
return 'abc';
}
};
obj.foo; // "abc"
语法改变
- 保留字(Reserved words)可以作为对象属性了(比如
new
、function
等)。
var obj = { new: 'abc' };
obj.new; // "abc"
- 合法的尾逗号(trailing commas)
允许在对象字面量和数组字面量上的使用尾逗号。
var obj = {
foo: 'bar',
};
var arr = [
1,
];
- 多行字符串
允许使用 \
转义行尾,实现多行字符串。
var str = 'I \
Love\
You';
标准库里的新功能
元编程
获得和设置原型
- Object.create()
- Object.getPrototypeOf()
管理属性特性
- Object.defineProperty()
- Object.defineProperties()
- Object.create()
- Object.getOwnPropertyDescriptor()
列举属性
- Object.keys()
- Object.getOwnPropertyNames()
Object 构造函数方法
- Object.preventExtensions()
- Object.isExtensible()
- Object.seal()
- Object.isSealed()
- Object.freeze()
- Object.isFrozen()
函数原型方法
- Function.prototype.bind()
新方法
- 字符串
- String.prototype.trim()
- 使用
[...]
访问字符
- 数组方法
- Array.isArray()
- Array.prototype.every()
- Array.prototype.filter()
- Array.prototype.forEach()
- Array.prototype.indexOf()
- Array.prototype.lastIndexOf()
- Array.prototype.map()
- Array.prototype.reduce()
- Array.prototype.some()
- 日期方法
- Date.now()
- Date.prototype.toISOString()
- JSON
- JSON.parse()
- JSON.stringify()
- 一些内置对象的
toJSON
方法- Boolean.prototype.toJSON()
- Number.prototype.toJSON()
- String.prototype.toJSON()
- Date.prototype.toJSON()
(完)
本作品采用《CC 协议》,转载必须注明作者和本文链接