Array.prototype.fill = function(value, start = 0, end = this.length) { // Handle negative values for start if (start < 0) { start += this.length; if (start < 0) start = 0; } // Handle negative values for end if (end < 0) { end += this.length; } // Ensure start is not greater than end if (start > end) { [start, end] = [end, start]; } // Fill the array for (let i = start; i < end; i++) { this[i] = value; } return this; }; 扩展阅读 Js中使用fill()进行数组填充的大坑,你遇到过吗? js数组怎么填充默认值