javascript padStart() 在 IE11 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51719553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 09:39:57  来源:igfitidea点击:

padStart() not working in IE11

javascriptangularjscross-browserinternet-explorer-11

提问by Nithin P.H

I'm using angularjs 1.7.2 and kendo ui scheduler. All routes are working fine in almost all browser except when it comes to padStart()part in IE 11. When padStartcode is taken this error shows up

我正在使用 angularjs 1.7.2 和 kendo ui scheduler。除了IE 11 中的padStart()部分外,几乎所有浏览器中的所有路由都可以正常工作。当采用padStart代码时,会出现此错误

TypeError: Object doesn't support property or method 'padStart'

类型错误:对象不支持属性或方法“padStart”

let ret = '#' + ((r << 16) + (g << 8) + b).toString(16).padStart(6, '0');

Is there a way we can handle this or an alternative way for implementing padStart

有没有办法我们可以处理这个或实现padStart的替代方法

回答by r3dst0rm

IE 11 is not supporting this function. Please take a look here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart#Browser_compatibility

IE 11 不支持此功能。请看这里:https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart#Browser_compatibility

What you are looking for are polyfills to fill up missing functions of your browser. The following code also taken from developer.mozilla.orgwill help you:

您正在寻找的是 polyfills 来填补浏览器缺失的功能。以下代码也来自developer.mozilla.org将帮助您:

// https://github.com/uxitten/polyfill/blob/master/string.polyfill.js
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
if (!String.prototype.padStart) {
    String.prototype.padStart = function padStart(targetLength,padString) {
        targetLength = targetLength>>0; //truncate 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);
        }
    };
}

Edit: As mentioned in the comments, by @Plaute, the function repeatneeds also to be polyfilled which can be found here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat

编辑:正如@Plaute 在评论中提到的,该函数repeat也需要被填充,可以在这里找到:https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String /重复

Or include this snippet:

或者包括这个片段:

if (!String.prototype.repeat) {
  String.prototype.repeat = function(count) {
    'use strict';
    if (this == null) {
      throw new TypeError('can\'t convert ' + this + ' to object');
    }
    var str = '' + this;
    count = +count;
    if (count != count) {
      count = 0;
    }
    if (count < 0) {
      throw new RangeError('repeat count must be non-negative');
    }
    if (count == Infinity) {
      throw new RangeError('repeat count must be less than infinity');
    }
    count = Math.floor(count);
    if (str.length == 0 || count == 0) {
      return '';
    }
    // Ensuring count is a 31-bit integer allows us to heavily optimize the
    // main part. But anyway, most current (August 2014) browsers can't handle
    // strings 1 << 28 chars or longer, so:
    if (str.length * count >= 1 << 28) {
      throw new RangeError('repeat count must not overflow maximum string size');
    }
    var maxCount = str.length * count;
    count = Math.floor(Math.log(count) / Math.log(2));
    while (count) {
       str += str;
       count--;
    }
    str += str.substring(0, maxCount - str.length);
    return str;
  }
}

Alternatively, to work around the String.prototype.repeat dependency, use the following line:

或者,要解决 String.prototype.repeat 依赖项,请使用以下行:

padString += Array.apply(null, Array(targetLength)).map(function(){ return padString; }).join("");