原文: https://hackernoon.com/es8-was-released-and-here-are-its-main-new-features-ee9c394adf66

-------------------------------------------------------------------------------------------------

This article originally appeared on dormoshe.io

This article was translated into Chinese by Jason ChengJapanese by PostdRussian by habrahabr and Korean by Han JaeYeab.

ECMAScript 8 or ECMAScript 2017 was officially released at the end of June by TC39. It seems that we are talking a lot about ECMAScript in the last year. It’s not for nothing. Currently, the standard is to publish a new ES specification version once a year. ES6 was published in 2015 and ES7 was published in 2016, but did someone remembered when ES5 was released? It happened in 2009, before the magical rise of JavaScript.

So we follow the changes in the development of JavaScript as a stable language, and now we need to enter ES8 to our lexicon.

 
 

If you are a strong person, take a deep breath and read the web or PDFedition of the specification. For the others, in this article, we will cover the main new features of ES8 by code examples.


String padding

This section adds two functions to the String object: padStart & padEnd.
As their names, the purpose of those functions is to pad the start or the end of the string, so that the resulting string reaches the given length. You can pad it with specific character or string or just pad with spaces by default. Here are the functions declarations:

str.padStart(targetLength [, padString])
str.padEnd(targetLength [, padString])

As you can see, the first parameter of those functions is the targetLength, that is the total length of the resulted string. The second parameter is the optional padString that is the string to pad the source string. The default value is space.

'es8'.padStart(2);          // 'es8'
'es8'.padStart(5); // ' es8'
'es8'.padStart(6, 'woof'); // 'wooes8'
'es8'.padStart(14, 'wow'); // 'wowwowwowwoes8'
'es8'.padStart(7, '0'); // '0000es8'
'es8’.padEnd(2);          // 'es8'
'es8’.padEnd(5); // 'es8 '
'es8’.padEnd(6, 'woof’); // 'es8woo'
'es8’.padEnd(14, 'wow’); // 'es8wowwowwowwo'
'es8’.padEnd(7, '6’); // 'es86666'
 

Browser support (MDN)


Object.values and Object.entries

The Object.values method returns an array of a given object’s own enumerable property values, in the same order as that provided by a for inloop. The declaration of the function is trivial:

Object.values(obj)

The obj parameter is the source object for the operation. It can be an object or an array (that is an object with indexes like [10, 20, 30] -> { 0: 10, 1: 20, 2: 30 }).

const obj = { x: 'xxx', y: 1 };
Object.values(obj); // ['xxx', 1] const obj = ['e', 's', '8']; // same as { 0: 'e', 1: 's', 2: '8' };
Object.values(obj); // ['e', 's', '8'] // when we use numeric keys, the values returned in a numerical
// order according to the keys
const obj = { 10: 'xxx', 1: 'yyy', 3: 'zzz' };
Object.values(obj); // ['yyy', 'zzz', 'xxx']
Object.values('es8'); // ['e', 's', '8']
 

Browser support (MDN) for Object.values

The Object.entries method returns an array of a given object's own enumerable property [key, value] pairs, in the same order as Object.values. The declaration of the function is trivial:

const obj = { x: 'xxx’, y: 1 };
Object.entries(obj); // [[’x’, 'xxx’], [’y’, 1]] const obj = [’e’, 's’, '8’];
Object.entries(obj); // [[’0’, 'e’], [’1’, 's’], [’2’, '8’]] const obj = { 10: 'xxx’, 1: 'yyy’, 3: 'zzz' };
Object.entries(obj); // [[’1’, 'yyy’], [’3’, 'zzz’], [’10’, 'xxx’]]
Object.entries('es8'); // [['0', 'e'], ['1', 's'], ['2', '8']]
 

Browser support (MDN) for Object.entries


Object.getOwnPropertyDescriptors

The getOwnPropertyDescriptors method returns all of the own properties descriptors of the specified object. An own property descriptor is one that is defined directly on the object and is not inherited from the object’s prototype. The declaration of the function is:

Object.getOwnPropertyDescriptors(obj)

The obj is the source object. The possible keys for the returned descriptor objects result are configurable, enumerable, writable, get, set and value.

const obj = { 
get es7() { return 777; },
get es8() { return 888; }
};
Object.getOwnPropertyDescriptors(obj);
// {
// es7: {
// configurable: true,
// enumerable: true,
// get: function es7(){}, //the getter function
// set: undefined
// },
// es8: {
// configurable: true,
// enumerable: true,
// get: function es8(){}, //the getter function
// set: undefined
// }
// }

The descriptor data is very important for advanced features like decorators.

 

Browser support (MDN)


Trailing commas in function parameter lists and calls

Trailing commas in function parameter is the ability of the compiler not to raise an error (SyntaxError) when we add an unnecessary comma in the end of the list:

function es8(var1, var2, var3,) {
// ...
}

As the function declaration, this can be applied on function’s calls:

es8(10, 20, 30,);

This feature inspired from the trailing of commas in object literals and Array literals [10, 20, 30,] and { x: 1, }.


Async functions

The async function declaration defines an asynchronous function, which returns an AsyncFunction object. Internally, async functions work much like generators, but they are not translated to generator functions.

function fetchTextByPromise() {
return new Promise(resolve => {
setTimeout(() => {
resolve("es8");
}, 2000);
});
}
async function sayHello() { 
const externalFetchedText = await fetchTextByPromise();
console.log(`Hello, ${externalFetchedText}`); // Hello, es8
}
sayHello();

The call of sayHello will log Hello, es8 after 2 seconds.

console.log(1);
sayHello();
console.log(2);

And now the prints are:

1 // immediately
2 // immediately
Hello, es8 // after about 2 seconds

This is because the function call doesn’t block the flow.

Pay attention that an async function always returns a promise and an await keyword may only be used in functions marked with the asynckeyword.

 

Browser support (MDN)


Shared memory and atomics

When memory is shared, multiple threads can read and write the same data in memory. Atomic operations make sure that predictable values are written and read, that operations are finished before the next operation starts and that operations are not interrupted. This section introduces a new constructor SharedArrayBuffer and a namespace object Atomics with static methods.

The Atomic object is an object of static methods like Math, so we can’t use it as a constructor. Examples for static methods in this object are:

  • add / sub— add / subtract a value for the value at a specific position
  • and / or / xor — bitwise and / bitwise or / bitwise xor
  • load — get the value at a specific position
 

Browser support (MDN)


And one for the next year in ES9 — Lifting template literal restriction

With the tagged template literal (ES6) we can do stuff like declaring a template parsing function and returning a value according to our logic:

const esth = 8;
helper`ES ${esth} is `;
function helper(strs, ...keys) {
const str1 = strs[0]; // ES
const str2 = strs[1]; // is
  let additionalPart = '';
if (keys[0] == 8) { // 8
additionalPart = 'awesome';
}
else {
additionalPart = 'good';
} return `${str1} ${keys[0]} ${str2} ${additionalPart}.`;
}

The returned value will be → ES 8 is awesome.
And for esth of 7 the returned value will be → ES 7 is good.

But there is a restriction for templates that contain for example \u or \x substrings. ES9 will deal with this escaping problem. Read more about it on the MDN website or on the TC39 document.

 

Browser support (MDN)


Conclusion

JavaScript is in production, but it is always getting renewed. The process of adopting new features to the specification is very arranged and poised. In the last stage, these features confirmed by the TC39 committee and implemented by the core developers. Most of them are already parts of the Typescript language, browsers or other polyfills, so you can go and try them right now.

 

ES8新特性——ES8 was Released and here are its Main New Features的更多相关文章

  1. ECMAScript 2017(ES8)新特性简介

    目录 简介 Async函数 共享内存和原子操作 Object的新方法 String的新方法 逗号可以添加到函数的参数列表后面了 简介 ES8是ECMA协会在2017年6月发行的一个版本,因为是ECMA ...

  2. es8 --- 新特性

    ES8尚未发布(2017年1月),下面是它已经完成起草的一些特性: Object.values() Object.entries() padStart() padEnd() Object.getOwn ...

  3. ES6/ES7/ES8新特性

    ES6 变量的改变 let const 2. 字符串新增方法 let str = 'react'; str.includes('re') // true str.repeat(3) // reactr ...

  4. ES8新特性

    Object.values/Object.entries Object.values和 Object.entries是在ES2017规格中,它和Object.keys类似,返回数组类型,其序号和Obj ...

  5. es6/es7/es8常用新特性总结(超实用)

    本文标题有误导性,因为我其实想写node8的新特性,说实话一下子从node v1.x跳跃到node 8.x+ 真有点受宠若惊的感觉.一直觉得node 数组. 对象.序列等的处理没有python方便,因 ...

  6. 细解JavaScript ES7 ES8 ES9 新特性

    题记:本文提供了一个在线PPT版本,方便您浏览 细解JAVASCRIPT ES7 ES8 ES9 新特性 在线PPT ver 本文的大部分内容译自作者Axel Rauschmayer博士的网站,想了解 ...

  7. ES6、ES7、ES8、ES9、ES10新特性

    ES6新特性(2015) ES6的特性比较多,在 ES5 发布近 6 年(2009-11 至 2015-6)之后才将其标准化.两个发布版本之间时间跨度很大,所以ES6中的特性比较多. 在这里列举几个常 ...

  8. ES6,ES7,ES8 常用特性总结

    一. ES6(ES2015) 1. 变量 let 和常量 const var 的问题 可以重复声明,没有报错和警告 无法限制修改 没有块级作用域, { } let 和 const 不能重复声明 都是块 ...

  9. ES7/8新特性学习随笔

    随着每年EcmaScript都会为js带来一些新特性,带来更多美化的编程体验,今天就走进一下es2016/2017所带来的新特性 ES7新特性 includes() 指数操作符 ES8新特性 asyn ...

随机推荐

  1. 编译 php-memcache 扩展时提示Cannot find autoconf

    下载memcache扩展 http://pecl.php.net/package/memcache ,到 /usr/local/src目录下并解压 [root@bogon src]# .tgz [ro ...

  2. USBDM RS08/HCS08/HCS12/Coldfire V1,2,3,4/DSC/Kinetis Debugger and Programmer -- BDM Construction and Firmware

    Construction. Build the hardware using the information provided in the PCB download. The following a ...

  3. ElasticSearch入门 :Windows下安装ElasticSearch

    这是ElasticSearch 2.4 版本系列的第一篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 E ...

  4. Golang 特性简介

    by sheepbao 主要大概介绍go语言的历史和特性,简单的入门. 来历 很久以前,有一个IT公司,这公司有个传统,允许员工拥有20%自由时间来开发实验性项目.在2007的某一天,公司的几个大牛, ...

  5. MongoDB 安装 Windows XP

    〇.  一个提供MonogoDB丰富资料的中文网站 http://www.cnblogs.com/hoojo/archive/2012/02/17/2355384.html 一. http://www ...

  6. 漏洞风险评估:CVSS介绍及计算

    CVSS 通用弱点评价体系(CVSS)是由NIAC开发.FIRST维护的一个开放并且能够被产品厂商免费采用的标准.利用该标准,可以对弱点进行评分,进而帮助我们判断修复不同弱点的优先等级. CVSS : ...

  7. jquery 实现table的行列选中效果改进

    行列都可以多选,也可对相应数据进行统计: 行选中效果 列选中效果

  8. 破产姐妹第一季/全集2 Broke Girls迅雷下载

    本季2 Broke Girls Season 1 (2011)看点:黑发泼辣的Max(凯特·戴琳斯 Kat Dennings 饰)在纽约布鲁克林区一家低档餐馆打工,餐馆同事包括小个子亚裔老板Han L ...

  9. Oracle初级性能优化总结

    前言 关于对Oracle数据库查询性能优化的一个简要的总结. 从来数据库优化都是一项艰巨的任务.对于大数据量,访问频繁的系统,优化工作显得尤为重要.由于Oracle系统的灵活性.复杂性.性能问题的原因 ...

  10. 用开源项目JazzyViewPager实现ViewPager切换动画

    JazzyViewPager这个项目可以让viewpager有各种绚丽的动画,而且还可以自由扩展.但从官网下载的lib导入时会出现找不到视图的问题,不知道是不是我人品不行,所以我就自己写了lib.总之 ...