Exactly clone an object in javascript
Posted
by Tom
on Stack Overflow
See other posts from Stack Overflow
or by Tom
Published on 2010-02-14T13:46:38Z
Indexed on
2010/03/21
1:11 UTC
Read the original article
Hit count: 309
Hi, I tried to exactly clone an object in javascript. I know the following solution using jquery:
var newObject = jQuery.extend({}, oldObject);
// Or
var newObject = jQuery.extend(true, {}, oldObject);
but the problem with that is, that the objects type gets lost:
var MyClass = function(param1, param2) {
alert(param1.a + param2.a);
};
var myObj = new MyClass({a: 1},{a: 2});
var myObjClone = jQuery.extend(true, {}, myObj);
alert(myObj instanceof MyClass); // => true
alert(myObjClone instanceof MyClass); // => false
Is there any solution to get true on the second alert?
© Stack Overflow or respective owner