ie不支持的方法实现

2021-12-07 16:37:17 浏览数 (2)

  • assign
代码语言:javascript复制
if (typeof Object.assign != 'function') {
    Object.assign = function (target) {
        'use strict';
        if (target == null) {
            throw new TypeError('Cannot convert undefined or null to object');
        }

        target = Object(target);
        for (var index = 1; index < arguments.length; index  ) {
            var source = arguments[index];
            if (source != null) {
                for (var key in source) {
                    if (Object.prototype.hasOwnProperty.call(source, key)) {
                        target[key] = source[key];
                    }
                }
            }
        }
        return target;
    };
}
  • startsWith/endsWith
代码语言:javascript复制
if (typeof String.prototype.startsWith != 'function') {
    String.prototype.startsWith = function (str) {
        if (str == null || str == '' || this.length == 0 || str.length > this.length)
            return false;
        return this.substr(0, str.length) == str;
    };
}
if (typeof String.prototype.endsWith != 'function') {
    String.prototype.endsWith = function (str) {
        if (str == null || str == '' || this.length == 0 || str.length > this.length)
            return false;
        return this.substring(this.length - str.length) == str;
    };
}

0 人点赞