123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- if (!Array.prototype.at) {
- // eslint-disable-next-line no-extend-native
- Object.defineProperty(Array.prototype, 'at', {
- value: function (index) {
- // 将索引转换为整数
- index = Math.trunc(index) || 0;
- // 如果索引为负数,则从数组末尾开始计算索引位置
- if (index < 0) {
- index += this.length;
- }
- // 如果索引越界,则返回 undefined
- if (index < 0 || index >= this.length) {
- return undefined;
- }
- // 返回指定索引位置的元素
- return this[index];
- },
- enumerable: false,
- configurable: true,
- writable: true
- });
- }
- if (typeof ArrayBuffer !== 'undefined' && !ArrayBuffer.hasOwnProperty('byteLength')) {
- Object.defineProperty(ArrayBuffer, 'byteLength', {
- get: function () {
- return this.byteLength || this.length;
- }
- });
- }
- if (typeof structuredClone === "undefined") {
- // 如果浏览器不支持 structuredClone 方法,则定义一个备用方法
- function structuredClone(obj) {
- if (obj === null || typeof obj !== "object") {
- return obj;
- }
- if (Array.isArray(obj)) {
- return obj.map(structuredClone);
- }
- if (obj instanceof ArrayBuffer || ArrayBuffer.isView(obj)) {
- return obj.slice(0);
- }
- const cloned = Object.create(Object.getPrototypeOf(obj));
- const props = Object.getOwnPropertyNames(obj);
- props.forEach((prop) => {
- const descriptor = Object.getOwnPropertyDescriptor(obj, prop);
- if (descriptor) {
- descriptor.value = structuredClone(descriptor.value);
- Object.defineProperty(cloned, prop, descriptor);
- }
- });
- return cloned;
- }
- }
|