Skip to content

Instantly share code, notes, and snippets.

@addyosmani
Last active May 31, 2016 19:16
Show Gist options
  • Save addyosmani/d5648c89420eb333904c to your computer and use it in GitHub Desktop.
Save addyosmani/d5648c89420eb333904c to your computer and use it in GitHub Desktop.
ES6 Array.prototype.fill() polyfill
if ( ![].fill) {
Array.prototype.fill = function( value ) {
var O = Object( this );
var len = parseInt( O.length, 10 );
var start = arguments[1];
var relativeStart = parseInt( start, 10 ) || 0;
var k = relativeStart < 0
? Math.max( len + relativeStart, 0)
: Math.min( relativeStart, len );
var end = arguments[2];
var relativeEnd = end === undefined
? len
: ( parseInt( end) || 0) ;
var final = relativeEnd < 0
? Math.max( len + relativeEnd, 0 )
: Math.min( relativeEnd, len );
for (; k < final; k++) {
O[k] = value;
}
return O;
};
}
@humanchimp
Copy link

is there a reason not to pass a radix to parseInt on line 14?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment