Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Deep clone
- function deepCopy(obj) {
- if(obj == null || typeof(obj) !== 'object'){
- return obj;
- }
- var deep = Object.create(obj.constructor.prototype); // or just: var deep = {};
- for(var key in obj){
- deep[key] = deepCopy(obj[key]);
- }
- return deep;
- }
- // Testing
- a = { test: 4, funk : function (){ return 5 } };
- b = deepCopy(a);
- a.test = 2;
- console.log(a); // a has changed value for test
- console.log(b); // b still has 4 as a value for test
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement