You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

40 line
630 B

  1. let cloneRecursive = function (o, newO) {
  2. if (typeof o !== 'object')
  3. return o;
  4. if (!o)
  5. return o;
  6. if (o instanceof Array) {
  7. if (!newO || !newO.push)
  8. newO = [];
  9. for (let i = 0; i < o.length; i++)
  10. newO[i] = cloneRecursive(o[i], newO[i]);
  11. return newO;
  12. }
  13. if (!newO)
  14. newO = {};
  15. for (let i in o) {
  16. if (o.hasOwnProperty(i))
  17. newO[i] = cloneRecursive(o[i], newO[i]);
  18. }
  19. return newO;
  20. };
  21. let clone = function (o) {
  22. try {
  23. let aLen = arguments.length;
  24. for (let i = 1; i < aLen; i++)
  25. cloneRecursive(arguments[i], o);
  26. } catch (e) {
  27. throw e;
  28. }
  29. return o;
  30. };
  31. module.exports = clone;