Nodejs Array Fill fill(value)

Here you can find the source of fill(value)

Method Source Code

Array.prototype.fill = function(value) {
    // Steps 1-2.
    if (this == null) {
        throw new TypeError('this is null or not defined');
    }//from  w  w  w  .  j  a  v  a  2s . co m

    var O = Object(this);

    // Steps 3-5.
    var len = O.length >>> 0;

    // Steps 6-7.
    var start = arguments[1];
    var relativeStart = start >> 0;

    // Step 8.
    var k = relativeStart < 0 ?
        Math.max(len + relativeStart, 0) :
        Math.min(relativeStart, len);

    // Steps 9-10.
    var end = arguments[2];
    var relativeEnd = end === undefined ?
        len : end >> 0;

    // Step 11.
    var final = relativeEnd < 0 ?
        Math.max(len + relativeEnd, 0) :
        Math.min(relativeEnd, len);

    // Step 12.
    while (k < final) {
        O[k] = value;
        k++;
    }

    // Step 13.
    return O;
};

Related

  1. fill( val )
    Array.prototype.fill = function( val ) {
      var len = this.length, i;
      if(val instanceof Array){
        for( i = len-1; i >= 0; i--){
          this.push(val);
      } else {
        for( i = 0; i < len; i++){
          this[i] = val;
    ...
    
  2. fill(first, last, value)
    Array.prototype.fill = function (first, last, value) {
        var i;
        for (i = first; i <= last; i++) {
            this[i] = value;
    
  3. fill(n, get)
    Array.prototype.fill = function(n, get)
        for (var i = 0; i < n; i++)
            this[i] = get(i)
        return this
    
  4. fill(n,v)
    var n = process.argv[2],
      base = process.argv[3],
      decoration = process.argv[4];
    Array.prototype.fill = function(n,v) {
        n = n || this.length;
        for( var i = 0; i < n; i++ )  {
            this[i] = v === undefined ? i : v;
        return this;
    ...
    
  5. fill(value)
    Array.prototype.fill = function (value) {
        var size = this.length;
        while (size > 0) {
            this[--size] = value;
        return this;
    };
    
  6. fill(x, y, v, starti, startj)
    Array.prototype.fill = function (x, y, v, starti, startj) {
      if ( ! starti ) {
        starti = 0;
      if ( ! startj ) {
        startj = 0;
      var matrix = this;
      for ( var i = starti; i <= x; i++ ) {
    ...