Saturday, 1 February 2014

Most efficient way to clone an object?

here doesn't seem to be an in-built one, you could try:
function clone(obj){
if(obj == null || typeof(obj) != 'object')
return obj;

var temp = obj.constructor(); // changed

for(var key in obj)
temp
[key] = clone(obj[key]);
return temp;
}
There's a lengthy post with many contributing comments on Keith Deven's blog.
If you want to stick to a framework, JQuery also has a clone() function:

// Clone current element
var cloned = $(this).clone();

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More