Exactly clone an object in javascript
- by Tom
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?