ECMAScript 2017 中的新特性:
字符串填充:padStart /padEnd
Object.entries
Object.values
异步函数
共享内存
1.padStart 字符串的开头
let str = '10';
str.padStart(3,0)
//'010'
str.padStart(4,0)
//'0010'
str.padStart(1,0)
//'10'
str.padStart(0,0)
//'10'
由此可以看出,padStart(参数1,参数2) 参数1是字符串前方补位,参数1代表小数点向左边移位,str.padStart(3,0) 数字3代表小数点向左边移动的位数,0代表是补位的数字
2.和padStart字符串同理,
let str = '10'
str.padEnd(3,2) //'102'
str.padEnd(5,2) //'10222'
由此可以看出,padEnd(参数1,参数2) 参数1是字符串后方补位,参数1代表小数点向有边移位,str.padEnd(3,2) 数字3代表小数点向右边移动的位数,2代表是补位的数字
注意:Internet Explorer 不支持字符串填充,好在IE死了,现在Edge 15版本即可兼容
3.Object.entries() 方法返回对象中键/值对的数组
const person = {
firstName : "Bill",
lastName : "Gates",
age : 50,
eyeColor : "blue"
};
eg1.
//使用entries遍历对象Object.entries(person)
结果如下:浏览器测试结果
(4) [Array(2), Array(2), Array(2), Array(2)]
0: (2) ['firstName', 'Bill']
1: (2) ['lastName', 'Gates']
2: (2) ['age', 50]
3: (2) ['eyeColor', 'blue']
length: 4
eg2.方便对象直接解构出每一组的key和value
const fruits = {Bananas:300, Oranges:200, Apples:500};
let text = "";
for (let [fruit, value] of Object.entries(fruits)) {
console.log(fruit, value)
}
//输出
VM1560:4 Bananas 300
VM1560:4 Oranges 200
VM1560:4 Apples 500
4.Object.values()
//取出对象的值的数组
const person = {
firstName : "Bill",
lastName : "Gates",
age : 50,
eyeColor : "blue"
};
console.log(Object.values(person))
//浏览器输出:
(4) ['Bill', 'Gates', 50, 'blue']
0: "Bill"
1: "Gates"
2: 50
3: "blue"
length: 4
- Async 函数
async function myDisplay() {
const values = await testGetValues();
console.log(values )
}
function testGetValues(){
//ajax请求逻辑....
return 请求结果数据
}