稻草网

ECMAScript 2017(ES8)新特性

Object.values/Object.entries – https://github.com/tc39/proposal-object-values-entries

字符串填充 String padding – https://github.com/tc39/proposal-string-pad-start-end

Object.getOwnPropertyDescriptors – https://github.com/ljharb/proposal-object-getownpropertydesc

Async functions – https://github.com/tc39/ecmascript-asyncawait

Shared memory and atomics – https://github.com/tc39/ecmascript_sharedmem

 

了解之后再补充

 

1. proposal-trailing-function-commas – https://github.com/tc39/proposal-trailing-function-commas 

函数参数列表和调用中的末尾逗号

新规范允许在函数定义的参数末尾,或者调用的参数末尾添加逗号,例如

function es8Comma(
foo,
bar,
baz,   //<--- 此处允许有逗号!
){   
   /* your code here... */
}

es8Comma(x,y,z,); //<--- 此处同样允许有逗号!

2.Object.values/Object.entries – https://github.com/tc39/proposal-object-values-entries

对象/数组遍历

 

Object.values()

类似于for…in,区别在于Object.values()只遍历可枚举且非原型链上的,参数自身的属性

如果传入参数是对象,则返回包含对象属性值的数组;如果传入参数是数组,则按原数组顺序返回原数组值,示例如下:

const obj = { foo: "bar", baz: 1 };
console.log(Object.values(obj)); // ['bar', 1]

const arr = ['e', 's', '8']; // 类似于 { 0: 'e', 1: 's', 2: '8' };
Object.values(arr); // ['e', 's', '8']

Object.values('es8'); // ['e', 's', '8']

 

Object.entries()

将对象中每组键值对按二维数组的形式返回

const obj = { foo: "bar", baz: 1 };
Object.entries(obj); // [['foo', 'bar'], ['baz', 1]]

const arr = ['e', 's', '8'];
Object.entries(arr); // [['0', 'e'], ['1', 's'], ['2', '8']]

Object.entries('es8'); // [['0', 'e'], ['1', 's'], ['2', '8']]

该方法可以将Object转化为真正的Map对象,例如:

var obj = { foo: 'bar', baz: 1 };
var map = new Map(Object.entries(obj));
map // Map { foo: "bar", baz: 1 }

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注