ES6 不断完善...

2018-06-05  本文已影响0人  静候那一米阳光

ECMAScript 2016

Array.prototype.includes

查找某个项是否在Array中(包括NaN)

const arr = [ 1, 'a', true, null, undefined, {}, NaN, Math];
arr.includes(1);//true
arr.includes('a');//true
arr.includes(true);//true
arr.includes(false);//false
arr.includes(null);//true
arr.includes(undefined);//true
arr.includes({});//false
arr.includes(NaN);//true
arr.includes(Math);//true
const arr = [];
arr.includes(1);//false
arr.includes('a');//false
arr.includes(true);//false
arr.includes(false);//false
arr.includes(null);//false
arr.includes(undefined);//false
arr.includes({});//false
arr.includes(NaN);//false
arr.includes(Math);//false

指数运算符

**

7**2 // 49

ECMAScript 2017

Object.keys() Object.entries()

Object.keys([1, 2, 3]) //["0","1","2"]
Object.entries([1, 2, 3]) //[["0",1],["1",2],["2",3]];
new Map(Object.entries([1, 2, 3])) // {"0" => 1, "1" => 2, "2" => 3}

String padding

'someString'.padStart(numberOfCharcters [,stringForPadding]);

'5'.padStart(10) // '          5'
'5'.padStart(10, '=*') //'=*=*=*=*=5'
'5'.padEnd(10) // '5         '
'5'.padEnd(10, '=*') //'5=*=*=*=*='

【栗子】

let date = new Date();
date.getFullYear().toString().padStart(4,'0');//年
now.getMonth()+1).toString().padStart(2,'0');//月
date.getDate().toString().padStart(2,'0'); //日
date.getHours().toString().padStart(2,'0'); //时
date.getMinutes().toString().padStart(2,'0'); //分
date.getSeconds().toString().padStart(2,'0'); //秒
上一篇 下一篇

猜你喜欢

热点阅读