PC版微信浏览器不支持padstart方法,提示:padStart is not a function

给String对象添加一个padStart方法解决此问题。

 (function () {
if (!String.prototype.padStart) {
String.prototype.padStart = function padStart(targetLength, padString) {
targetLength = targetLength >> 0; //floor if number or convert non-number to 0;
padString = String(typeof padString !== 'undefined' ? padString : ' ');
if (this.length > targetLength) {
return String(this);
} else {
targetLength = targetLength - this.length;
if (targetLength > padString.length) {
padString += padString.repeat(targetLength / padString.length); //append to original to ensure we are longer than needed
}
return padString.slice(0, targetLength) + String(this);
}
};
}
})()

参考:

https://developers.weixin.qq.com/community/develop/doc/00024242d282a8be03a7c9d9d5b400?_at=1563258721677

https://blog.csdn.net/qq_36938056/article/details/104569905

https://github.com/behnammodi/polyfill/blob/master/string.polyfill.js

发表评论

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