解决企业微信提示String(...).padStart is not a function(…)错误

String(...).padStart is not a function(…)

这是因为企业微信内核版本太低不支持padStart这个方法,在User-Agent参数里内核是Chrome/53.0.2785.116
既然企业微信不支持padStart(),那么我们只能自己在String的原型上添加方法了。
参考下面代码,将下面的代码添加到head里。注意一定要放到head里让他在body之前被执行。

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);
        }
    };
}

你可能感兴趣的:(解决企业微信提示String(...).padStart is not a function(…)错误)