Clone Javascript Array

The Correct Way to Clone Javascript Arrays

var clone = originalArray.slice(0);

Array.prototype.clone = function() { return this.slice(0); }

Object.prototype.clone =function(){
  var myObj =(thisinstanceofArray)?[]:{};
  for(i inthis){
    if(i !='clone'){
        if(this[i]&&typeofthis[i]=="object"){
          myObj[i]=this[i].clone();
        }else 
            myObj[i]=this[i];
        } 
    }
  return myObj;
};

 

 

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s