To easily swap the properties, you can use the following code:
MyObject.prototype.doIt = function()
{
var a = this.obj1;
this.obj1 = this.obj2;
this.obj2 = a;
}
Even though obj1
and obj2
are not primitive types, their type does not matter in this swapping process.
If there are existing references to this.obj1
or this.obj2
outside of this code, those references will not be swapped.
Completely swapping this.obj1
and this.obj2
, including existing references, is not possible. You could remove all properties from one object, move them to the other object, and vice versa, but you cannot alter the prototypes of objects to fundamentally swap their identities.