> Ah ok... I just thought that ECMAScript/ActionScript was purely a
> prototype-based language, hence simulating inheritance with the
> __proto__ chain. If you are sure that changing the __proto__ slot of
> an object doesn't affect its inheritance tree, then what you state
> would be the best solution (ie. composition).
changing an object's __proto__ does affect its inheritance chain. very
much so. but that's alright, as Jos already pointed out.
just to clarify, here's what's happening:
you have your movieclip instance, test_mc, created via
createEmptyMovieClip, so its class is MovieClip and test_mc.__proto__
== MovieClip.prototype. and you have your Cow class, which has
MovieClip as a superclass, which means that Cow.prototype.__proto__ ==
MovieClip.prototype.
now, if you point test_mc.__proto__ to Cow.prototype, then
test_mc.__proto__.__proto__ == MovieClip.protoype. so, in effect, what
you do by assigning Cow.prototype to test_mc.__proto__ is to insert an
extra link into the prototype chain while still having
MovieClip.protoype at the end. that's a perfectly fine thing to do --
but it does effect the inheritance chain, which is what you want.
it really isn't as complicated as it may look like at first glance.
here's how inheritance in flash works in a nutshell:
classes actually are constructor functions named like the class name,
e.g. function Foo(). that constructor function has a protoype property
(Foo.prototype), pointing to an object that has all properties
instances of that class inherit. every class has a superclass -- if
you don't specify it, then the superclass is Object.
every object has a __proto__ property, pointing to another object it
inherits from. if you try to access an object's property, the flash
virtual machine first checks if the object has that property, and if
not, continues looking for it in the object's __proto__ until it
either finds the property or ends up with Object.prototype. put
differently, here's how you would do it "by hand" if the VM wouldn't
do it for you:
var accessProperty = function ( property:String, object:Object ) {
if( object[ property ] !== undefined ) {
// check if object has the property
return object[ property ];
} else if( object != Object.prototype ) {
// try nect object in the inheritance chain
accessProperty( property, object.__proto__ );
} else {
// object is Object.prototype, end of inheritance chain
// property does not exist
return undefined;
}
}
(actually, in the end the original object's __resolve method would be
called if it exists, with the non-existant property passed as a
string. whatever __resolve returns is used as the property's value)
so how does "new" work then?
say you have a class/constructor function Foo. when you call new
Foo(), a new object is created, gets a property __proto__ set to
Foo.prototype, and then the constructor is executed with "this" set to
the new object.
how do superclasses work?
if Foo extends Bar, then Foo.prototype.__proto__ points to Bar.prototype.
that has someinteresting consequences:
if you for some reason at one point in you app set
Foo.prototype.__proto__ to Object.prototype, then Foo does not extend
Bar anymore. that wouldn't only affect any new instances of Foo, but
all previously instanciated instances, too.
if you have an instance of Foo named myFoo, and you set
myFoo.__proto__ to Bar.prototype, then myFoo does not inherit from Foo
any more, but from Bar directly. you could even create an anonymouse
object myObj = {}, and set myObj.__proto__ to Foo.prototype or
Bar.prototype depending on what kind of behaviour you need at the
moment. in fact, it doesn't even have to inherit from a class
prototype at all, __proto__ can point to any object you like, and you
can change it any time you like.
needles to mention, the examples in the last paragraph would be
painfully bad practice, i only listed them to better illustrate how it
works.
hth,
mark
--
http://snafoo.org/
jabber: [EMAIL PROTECTED]
_______________________________________________
osflash mailing list
[email protected]
http://osflash.org/mailman/listinfo/osflash_osflash.org