(1)從前填充:str.padStart(targetLength [, padString]);javascript
示例: 'es8'.padStart(7, '0'); // '0000es8'
(2)從後填充:str.padEnd(targetLength [, padString])java
示例: 'es8'.padEnd(7, '6'); // 'es86666'
(1)Object.values(obj);
數組
Object.values 函數會返回指定對象的可枚舉的屬性值數組,數組中值順序與 for-in 循環保持一致;
異步
首個參數 obj 即爲須要遍歷的目標對象,它能夠爲某個對象或者數組(數組能夠看作鍵爲下標的對象):async
示例:
const obj = ['e', 's', '8']; // same as { 0: 'e', 1: 's', 2: '8' };
Object.values(obj); // ['e', 's', '8']
(2)Object.entries(obj);
函數
Object.entries 方法則會將某個對象的可枚舉屬性與值按照二維數組的方式返回,數組中順序與 Object.values 保持一致,該函數的聲明與使用爲:fetch
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']]
四種使用方式:spa
async function foo() {}
const foo = async function() {}
let obj = { async foo() {} }
const foo = async () => {}
ES8 中容許使用 async/await 語法來定義與執行異步函數,async 關鍵字會返回某個 AsyncFunction 對象;在內部實現中雖然異步函數與迭代器的實現原理相似,可是其並不會被轉化爲迭代器函數:code
function fetchTextByPromise() {
return new Promise(resolve => {
setTimeout(() => {
resolve("es8");
}, 2000);
});
}
async function sayHello() {
const externalFetchedText = await fetchTextByPromise();
console.log(`Hello, ${externalFetchedText}`); // Hello, es8
}
sayHello();
console.log(1);
sayHello();
console.log(2);
// 調用結果
1 // immediately
2 // immediately
Hello, es8 // after 2 seconds