Posted By

remysharp on 07/24/07


Tagged

object clone


Versions (?)


Advertising

Website Promotion DIRECTORY is a crucial factor for all websites that need to gain better organic search engine rankings and increase website traffic.
Submitting your website as part of your Web Promotion strategy to our SEO friendly and high traffic Business Directory for review is an excellent way to gain a valuable backlink and increase your websites visibility online.

Submit Site


clone


Published in: JavaScript 






Clones and object (associative array - not arrays). Goes deep.

Required sometimes when you need to keep the original object intact, i.e. when you pass an object in to a function and modify it in the function - the original would change.

Expand | Embed | Plain Text
  1. function clone(o) {
  2. if(typeof(o) != 'object') return o;
  3. if(o == null) return o;
  4.  
  5. var newO = new Object();
  6.  
  7. for(var i in o) newO[i] = clone(o[i]);
  8.  
  9. return newO;
  10. }

Report this snippet 

Comments

RSS Icon Subscribe to comments
Posted By: KitSunde on May 29, 2008

Line 2,3 could more effectively be written as: if(typeof(o)!="object"||o==null)return o;

You need to login to post a comment.

Download royalty free graphics