Advertisement
SmaJli

deepClone_NVD

Nov 16th, 2018
588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.49 KB | None | 0 0
  1. // Deep clone
  2. function deepCopy(obj) {
  3. if(obj == null || typeof(obj) !== 'object'){
  4. return obj;
  5. }
  6.  
  7. var deep = Object.create(obj.constructor.prototype); // or just: var deep = {};
  8.  
  9. for(var key in obj){
  10. deep[key] = deepCopy(obj[key]);
  11. }
  12. return deep;
  13. }
  14.  
  15. // Testing
  16.  
  17. a = { test: 4, funk : function (){ return 5 } };
  18. b = deepCopy(a);
  19. a.test = 2;
  20. console.log(a); // a has changed value for test
  21. console.log(b); // b still has 4 as a value for test
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement