基本语法&用法
str.padStart(targetLength [, padString])
- targetLength: 目标字串被填充后的长度。如果此参数小於原字串的长度,那將直接返回原字串
- padString: 用來填充的字串
let str = '123';
console.log(str.padStart(5, 0)) //00123
console.log(str.padEnd(5, 0)) //12300
应用
日期填充 & 手机号码显示后四位
let now = new Date();
let year = now.getFullYear();
let month = now.getMonth() + 1
let day = now.getDay()
console.log(year) //2020
console.log(month.toString().padStart(2, 0)) //11
console.log(day.toString().padStart(2, 0))//03
let tel = '13713685499';
console.log(tel.slice(-4).padStart(13, '*'))//*********5499
网友评论