Javascript Array extend(other_array)

Description

Javascript Array extend(other_array)


Array.prototype.extend = function(other_array) {
  if(other_array.constructor !== Array) throw other_array + ' is not an array.';

  other_array.forEach(function(v) {
    this.push(v)// ww  w  .  j  a v  a2  s. c  o m
  }, this);
}

Javascript Array extend(other_array)

Array.prototype.extend = function(other_array) {
    other_array.forEach(function(v) {
        this.push(v)/*from  ww w .  j a v a  2s. co m*/
    }, this);
}

Javascript Array extend(other_array)

/**/* w  w w  .j  a  v  a2 s  .  c om*/
  *This file adds some useful helper functions/prototypes that make things
  * easier.
 */

/** Adds an 'extend' method to the array type that allows you to extend
  * an array. */
Array.prototype.extend = function (other_array) {
    /* you should include a test to check whether other_array really is an array */
    other_array.forEach(function(v) {this.push(v)}, this);
}

/** Adds a 'clone' method to the objects. */
Object.prototype.clone = function() {
  var newObj = (this instanceof Array) ? [] : {};
  for (i in this) {
    if (i == 'clone') continue;
    if (this[i] && typeof this[i] == "object") {
      newObj[i] = this[i].clone();
    } else newObj[i] = this[i]
  } return newObj;
};



PreviousNext

Related