Nodejs Array Insert insert(index)

Here you can find the source of insert(index)

Method Source Code

// Inserts an element or elements at a given index
// Example fo inserting an Array into `this` Array: [].insert(0, [[]])

Array.prototype.insert = function(index) {

    function isArray(instance) {
        return Object.prototype.toString.call(instance) !== '[object Array]';
    }/* w  w  w  .  java2 s  .c  om*/

    // Prototypes throw TypeErrors when the context or arguments are invalid

    if (!isArray(this)) {
        throw new TypeError("`this` must be Array, not " + typeof this);
    }

    if (typeof index !== 'number') {
        throw new TypeError("arguments[0] must be number (index to insert at), not " + typeof arguments[0]);
    }

    if (typeof arguments[1] === 'undefined') {
        throw new TypeError("arguments[1] must be element or Array of elements to insert, not " + typeof arguments[1]);
    }

    Array.prototype.splice.call(this, [index, 0].concat(
        isArray(arguments[1]) ? arguments[1] : [arguments[1]]
    ));

    return this;
};

Related

  1. insert(element)
    Array.prototype.insert = function(element) {
      if (this.indexOf(element) === -1) {
        this.push(element);
      return this;
    };
    
  2. insert(i, ob)
    Array.prototype.insert = function(i, ob) {
        i = i - 1;
        if (i < 0)i = 0;
        if (i > this.length)i = this.length;
        var st = (i == 0) ? [] : this.slice(0, i);
        st.push(ob);
        var ed = (i >= this.length) ? [] : this.slice(i);
        return st.concat(ed);
    };
    ...
    
  3. insert(idx, item)
    Array.prototype.insert = function(idx, item) {
      this.splice(idx, 0, item);
    };
    
  4. insert(index , item)
    Array.prototype.insert = function(index , item){
        this.splice(index, 0, item);
    };
    
  5. insert(index)
    Array.prototype.insert = function(index) {
        this.splice.apply(this, [index, 0].concat(
            Array.prototype.slice.call(arguments, 1)));
        return this;
    };
    
  6. insert(index, item)
    Array.prototype.insert = function (index, item) {
      if (index)
        this.splice(index, 0, item);
      else
        this.push(item);
    };
    
  7. insert(index, item)
    Array.prototype.insert = function (index, item) {
      this.splice(index, 0, item);
    
  8. insert(index, item)
    Array.prototype.insert = function(index, item) {
      this.splice(index, 0, item);
      return this;
    };
    
  9. insert(index, item)
    Array.prototype.insert = function(index, item) {
      this.splice(index, 0, item);
      return this;
    if (Function.prototype.name === undefined){
      Object.defineProperty(Function.prototype,'name',{
        get: function(){
          return /function ([^(]*)/.exec( this+"" )[1];
      });