Javascript Object copy()

Description

Javascript Object copy()



function copy(object) {
  if (!object || typeof (object) != 'object' || object instanceof Class) {
    return object;
  } else if (object instanceof Array) {
    var c = [];//from  ww  w . java2s. co  m
    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;
  }
}



PreviousNext

Related