PDA

View Full Version : doubt with instanceof vs __proto__ in as2.0 and as3.0



arnab
January 30th, 2010, 03:43 PM
Hello Friends

----------------- in as 2.0 -------------

while trying oop the inheritance and not the composition way I tried two different ways to find out whether an object ( lets say circleclip1 which is a Movieclip in library with an identifier provided )formed from a class (lets say circleDemo)which extends MovieClip is truly an instance of that class. I use the

trace(circleclip1.__proto__==circleDemo.prototype) to check the same

it outputs true in the trace window as it should.

but when I use the instanceof operator it would return true no matter whichever ancestor I use to check it against the object instance..eg


trace(circleclip1 instanceof circleDemo);// yields "true"
trace(circleclip1 instanceof MovieClip);// checking against Movieclip yields "true"
trace(circleclip1 instanceof Object);// checking against top level Object yields "true"

Now my question is why does this instanceof operator return true for any ancestor up the inheritance
chain uptill the top level object.

while when I use

trace(circleclip1.__proto__==MovieClip.prototype);// yields "false"

so the verification implies circleclip1 is not directly an instance of MovieClip since it has been subclassed through circleDemo hence the output false.


----------------- in as 3.0 -------------


how would I test whether in as3.0 circleclip1 is truly an instance of circleDemo....because instanceof
operator keeps yielding the same result as as2.0

and frustratingly enough that __proto__ property of any object is not around any more in as3.0
so can someone enlighten me on how to do this check in as 3.0 please ??


I have attached two .zip files of as2.0 and as3.0 version containing the demonstration of the problem queried above respectively, as printing all the lines from those files here in this post would clutter
up the area.

Thanks in advance.
Arnab

_kp
January 30th, 2010, 04:21 PM
instanceOf may still work but there is an new keyword "is" to replace it.
http://www.kirupa.com/forum/showthread.php?p=1914650

There also is a .constructor property for every object that returns the exact class it belongs to.

Krilnon
January 30th, 2010, 05:10 PM
You can also use flash.utils.getQualifiedClassName, too.

TheCanadian
January 30th, 2010, 06:13 PM
Hello Friends

----------------- in as 2.0 -------------

while trying oop the inheritance and not the composition way I tried two different ways to find out whether an object ( lets say circleclip1 which is a Movieclip in library with an identifier provided )formed from a class (lets say circleDemo)which extends MovieClip is truly an instance of that class. I use the

trace(circleclip1.__proto__==circleDemo.prototype) to check the same

it outputs true in the trace window as it should.

but when I use the instanceof operator it would return true no matter whichever ancestor I use to check it against the object instance..eg


trace(circleclip1 instanceof circleDemo);// yields "true"
trace(circleclip1 instanceof MovieClip);// checking against Movieclip yields "true"
trace(circleclip1 instanceof Object);// checking against top level Object yields "true"

Now my question is why does this instanceof operator return true for any ancestor up the inheritance
chain uptill the top level object.

while when I use

trace(circleclip1.__proto__==MovieClip.prototype);// yields "false"

so the verification implies circleclip1 is not directly an instance of MovieClip since it has been subclassed through circleDemo hence the output false.
circleclip1 is an instance of MovieClip and Object, which is why instanceof returns true. The reason that your prototype check is false is that the equality operator looks for two identical objects (ie the same object in memory). And, even though the circleDemo prototype contains all of the properties of the MovieClip prototype (in fact, circleDemo.prototype is an instance of MovieClip), they are not the same object.