Last active
May 31, 2016 19:16
-
-
Save addyosmani/d5648c89420eb333904c to your computer and use it in GitHub Desktop.
ES6 Array.prototype.fill() polyfill
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
is there a reason not to pass a radix to
parseInt
on line 14?