Nodejs Object Clone copy(object)

Here you can find the source of copy(object)

Method Source Code

function copy(object) {
    if (/*from w  ww.jav  a 2s  .  co m*/
            !object || typeof (object) != 'object' ||
                    object instanceof Class
            ) {
        return object;
    }
    else if (object instanceof Array) {
        var c = [];
        for (var i = 0, l = object.length; i < l; i++) {
            c[i] = copy(object[i]);
        }
        return c;
    }
    else {
        var c = {};
        for (var i in object) {
            c[i] = copy(object[i]);
        }
        return c;
    }
}

Related

  1. clone()
    Object.prototype.clone = function () {
      var obj = this.constructor === Array ? [] : {};
      for (var e in this) {
        obj[e] = typeof this[e] === 'object' ? this[e].clone() : this[e];
      return obj;
    
  2. clone()
    Object.prototype.clone = function() {
      var newObj = {};
      for (var i in this) {
        if (typeof(this[i]) == 'object' || typeof(this[i]) == 'function') {
          newObj[i] = this[i].clone();
        } else {
          newObj[i] = this[i];
      return newObj;
    };
    
  3. clone()
    Object.prototype.clone = function () {
      var o = new Object(); 
      for (var property in this) {
        o[property] = typeof (this[property]) == 'object' ? this[property].clone() : this[property]
      return o
    
  4. clone(object)
    Object.clone = function(object) {
      var clone = {};
      Object.each(object, function(key, value) {
        clone[key] = value;
      });
      return clone;
    };
    
  5. cloneclone()
    Object.prototype.clone = function clone() {
      function F() {}
      F.prototype = this;
      return new F();