Behaviour of HasInstance() and FindInstanceInPrototypeChain() after a SetPrototype()

36 views
Skip to first unread message

Moritz Möller

unread,
Aug 22, 2015, 7:10:27 AM8/22/15
to v8-users
Hi,

I'm trying to modify the prototype of a created object inside the constructor to change it's class (whether this is a good or bad idea).

So I have a FunctionTemplate "baseClass" which has a call handler baseClassConstructor.
Then there is a FunctionTemplate "subClass" which has subClass->Inherit(baseClass) called.

Now in the baseClassConstructor there are some circumstances where I want the created object to have the prototype of subClass.

What I do inside the constructHandler:

Object temporaryClass = subClass->GetFunction()->NewInstance();
info
.This()->SetPrototype( temporaryClass->GetPrototype() );
info
.This()->Set( "constructor", temporaryClass->Get("constructor") );



That pretty much works, with one problem:

subClass->HasInstance(temporaryClass) -> returns true as expected.
subClass
->HasInstance(info.This()) -> returns false (which might be okay - I don't know if HasInstance() actually uses the prototype)



and:

temporaryClass->FindInstanceInPrototypeChain(subClass) -> returns !empty
info
.This()->FindInstanceInPrototypeChain(subClass) -> returns empty - but here subClass should be found in the prototype chain previously set?



Maybe someone can point me in the right direction why it is this way and if it's supposed to be this way.

Thank you,

Moritz



Reply all
Reply to author
Forward
0 new messages