Just out of curiosity, and to make a little experiment I have in mind, is it possible to change the type of an object after it's been created?
By changing it's type I mean to make the object respond to the instanceof operator, like this:
(as you can see, I tried changing the constructor, but it didn't work)
var MyConstructor = function() { this.name = 'no name' };
undefined
var myInstance = { name: 'some name' }
undefined
myInstance instanceof Object
true
myInstance instanceof MyConstructor
false
myInstance.constructor
function Object() { [native code] }
myInstance.constructor = MyConstructor
function () { this.name = 'no name' }
myInstance instanceof Object
true
myInstance instanceof MyConstructor
false
I guess I could create a new object with new MyConstructor(), and then delete every property from it and then copy every property from myInstance, but was wondering if there exists a simpler way...
-- UPDATE --
I do know the existence of the __proto__
property, I just discarded it upfront because I know that event though it's implemented in most browsers is not part of the standard.