/** * compat.js — 老浏览器标准库补丁 * * 本文件补的是 ES2015+ 的“语言标准库方法”,IE11 及同期浏览器没有: * String: padStart(ES2017) / startsWith / endsWith / includes(ES2015) * Array: includes(ES2016) * Object: assign(ES2015) * * 注意: * 1. 文件用 ES5 语法书写(IE11 不认识箭头函数、let/const), * 但这指的是写法,不是补 ES5 的能力 —— 补的均是 ES2015+ 的方法。 * 2. Promise(ES2015) / fetch / EventSource 属于运行时与平台 API, * 不在本文件,由同目录 promise.min.js、fetch.min.js、 * eventsource.min.js 提供;三者在加载时检测原生能力,存在则不覆盖。 * 3. 本文件不依赖上述三者,但必须在业务 JS 之前加载。 * 4. downloadBlobFile() 保留在 util.js,不放在这里。 */ (function () { "use strict"; /* ========================================================= * String * ========================================================= */ if (!String.prototype.padStart) { String.prototype.padStart = function (targetLength, padString) { var str = String(this); var len = Number(targetLength) || 0; var pad = padString === undefined ? " " : String(padString); var need = len - str.length; var result = ""; if (need <= 0) return str; if (pad === "") return str; while (result.length < need) { result += pad; } return result.slice(0, need) + str; }; } if (!String.prototype.startsWith) { String.prototype.startsWith = function (search, position) { var str = String(this); var pos = Number(position) || 0; search = String(search); if (pos < 0) pos = 0; return str.substr(pos, search.length) === search; }; } if (!String.prototype.endsWith) { String.prototype.endsWith = function (search, length) { var str = String(this); search = String(search); if (length === undefined || length > str.length) { length = str.length; } length = Number(length); if (isNaN(length) || length < 0) length = 0; return str.substring(length - search.length, length) === search; }; } if (!String.prototype.includes) { String.prototype.includes = function (search, start) { if (search instanceof RegExp) { throw new TypeError("不支持将 RegExp 作为 includes 参数"); } return String(this).indexOf(String(search), Number(start) || 0) !== -1; }; } /* ========================================================= * Array * ========================================================= */ if (!Array.prototype.includes) { Array.prototype.includes = function (search, fromIndex) { var arr = Object(this); var len = arr.length >>> 0; var i = Number(fromIndex) || 0; if (!len) return false; // 按 ToInteger 向零截断,否则 fromIndex 为负小数时会从错误下标起扫 i = i < 0 ? Math.ceil(i) : Math.floor(i); if (i < 0) { i = Math.max(len + i, 0); } for (; i < len; i++) { if (arr[i] === search || (arr[i] !== arr[i] && search !== search)) { return true; } } return false; }; } /* ========================================================= * Object * ========================================================= */ if (!Object.assign) { Object.assign = function (target) { if (target == null) { throw new TypeError("Object.assign 的目标不能为 null 或 undefined"); } var to = Object(target); var i; var source; var key; for (i = 1; i < arguments.length; i++) { source = arguments[i]; if (source == null) continue; source = Object(source); for (key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { to[key] = source[key]; } } } return to; }; } })();