????喜大普奔,ES2019登场

栏目: JavaScript · 发布时间: 5年前

内容简介:就在刚4个小时前,TC39将以下特性加入到了 ES2019 中。让我们来看看这些新的特性给我们带来了什么样的改变。:arrow_right: Array#{flat,flatMap}:arrow_right: Object.fromEntries

就在刚4个小时前,TC39将以下特性加入到了 ES2019 中。让我们来看看这些新的特性给我们带来了什么样的改变。

????喜大普奔,ES2019登场

ES2019 新特性:

:arrow_right: Array#{flat,flatMap}

:arrow_right: Object.fromEntries

:arrow_right: String#{trimStart,trimEnd}

:arrow_right: Symbol#description

:arrow_right: try { } catch {} // optional binding

:arrow_right: JSON ⊂ ECMAScript

:arrow_right: well-formed JSON.stringify

:arrow_right: stable Array#sort

:arrow_right: revised Function#toString

JSON ⊂ ECMAScript (JSON superset)

行分隔符(U + 2028)和段分隔符(U + 2029)符号现在允许在字符串文字中,与JSON匹配。 以前,这些符号在字符串文字中被视为行终止符,因此使用它们会导致SyntaxError异常。

well-formed JSON.stringify

更加友好的 JSON.stringify (修复了对于一些超出范围的 unicode 展示错误的问题。)

如果输入 Unicode 格式但是超出范围的字符,在原先JSON.stringify返回格式错误的Unicode字符串:

JSON.stringify('\uD800');
// → '"�"'
复制代码

现在实现了一个改变JSON.stringify的 第3阶段提案 ,因此它为其输出转义序列,使其成为有效Unicode(并以UTF-8表示):

JSON.stringify('\uD800');
// → '"\ud800"'
复制代码

stable Array#sort

在以前,sort 函数中,10个以上元素的数组 V8 使用不稳定的QuickSort(快排。现在,使用稳定的TimSort算法。)

TimSort算法: en.wikipedia.org/wiki/Timsor…

revised Function#toString

Function.prototype.toString() 现在返回精确字符,包括空格和注释。原先和现在的比较:

// Note the comment between the `function` keyword
// and the function name, as well as the space following
// the function name.
function /* a comment */ foo () {}

// Previously:
foo.toString();
// → 'function foo() {}'
//             ^ no comment
//                ^ no space

// Now:
foo.toString();
// → 'function /* comment */ foo () {}'

复制代码

Array #{flat, flatMap}

数组降维,递归地将数组展平到指定的深度,默认为1。

// Flatten one level:
const array = [1, [2, [3]]];
array.flat();
// → [1, 2, [3]]

// Flatten recursively until the array contains no more nested arrays:
array.flat(Infinity);
// → [1, 2, 3]
复制代码
[2, 3, 4].flatMap((x) => [x, x * 2]);
// → [2, 4, 3, 6, 4, 8]
复制代码

Object.fromEntries

Object.fromEntries(Object.entries(object))≈ 对象

它类似于Lodash的_.fromPairs。

????喜大普奔,ES2019登场

String#{trimStart,trimEnd}

前后的空白符可以指定一边去除。

const string = '  hello world  ';
string.trimStart();
// → 'hello world  '
string.trimEnd();
// → '  hello world'
string.trim();
// → 'hello world'
复制代码

Symbol.prototype.description

通过工厂函数Symbol()创建符号时,您可以选择通过参数提供字符串作为描述:

const sym = Symbol('The description');
复制代码

以前,访问描述的唯一方法是将符号转换为字符串:

assert.equal(String(sym), 'Symbol(The description)');
复制代码

现在引入了getter Symbol.prototype.description以直接访问描述:

assert.equal(sym.description, 'The description');
复制代码

以上所述就是小编给大家介绍的《????喜大普奔,ES2019登场》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Speed Up Your Site

Speed Up Your Site

Andrew B. King / New Riders Press / 2003-01-14 / USD 39.99

There's a time bomb on the web: user patience. It starts ticking each time someone opens one of your pages. You only have a few seconds to get compelling content onto the screen. Fail, and you can kis......一起来看看 《Speed Up Your Site》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具