Skip to content

Instantly share code, notes, and snippets.

@allenwb allenwb/ArrayFill.js
Last active Dec 15, 2015

What would you like to do?
Array.prototype.fill = function fill(value,start=0, end=this.length) {
Every element of array from start up to but not including end are assigned value.
start and end are interpretation the same as for slice,
negative start or stop indices are converted to positive indices relative to the lenth of the array.
If end<=start no elements are modified.
If end>this.length and this.length is read-only a range error is thrown and no elements are modified.
If end>this.length and this.length is not read-only, this.length is set to end.
Array elements are set sequntially starting with the start index.
If an element is encountered that cannot be assigned, a type error is thrown and no other elements are modified
The array is return as the value of this method
//the following is not yet complete
var t = Object(this);
var len = Math.max(0,Math.trunc(t.length));
var relativeStart =
if (this == null) {
throw new TypeError();
var n = 0;
if (arguments.length > 1) {
n = Number(arguments[1]);
if (n != n) { // shortcut for verifying if it's NaN
n = 0;
} else if (n != 0 && n != Infinity && n != -Infinity) {
n = (n > 0 || -1) * Math.floor(Math.abs(n));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.