> Where could I find a nice tutorial or just more information about this
> "mystic" AS1 inheritance stuff? Any book sugestions?
it's not mystic at all, but the prototype chain. each object (except
for Object.prototype) has a __proto__ property pointing to another
object it inherits from. a class instance's __proto__ always points to
the class' prototype. that prototype in turn has a __proto__ pointing
to the superclass' prototype, and so on, until you eventually end up
at Object.prototype.
say you have a class Foo that extends Bar (which inherits directly
from Object). you create an instance myFoo. then the below are true:
myFoo.__proto__ == Foo.prototype
Foo.prototype.__proto__ == Bar.prototype
Bar.prototype.__proto__ == Object.prototype
therefore also:
myFoo.__proto__.__proto__ == Bar.prototype
Foo.prototype.__proto__.__proto__ == Object.prototype
and so on.
when you try to read a property of an object (that includes calling a
method, of course), first the VM checks if your object has that
property. if not, it checks its __proto__, then the __proto__'s
__proto__ until it either finds that property or it ends up at
Object.prototype, at which point your instance's __resolve method will
be called (if it exists).
so, instead of
var myFoo = new Foo( "bar" );
you could also
var myFoo = {};
myFoo.__proto__ = Foo.prototype;
myFoo.constructor = Foo;
myFoo.constructor( "bar" );
which would be the same thing.
that means you *could* do evil stuff like changing an object's
inheritance on the fly. if you point myFoo.__proto__ to Bar.prototype
it won't inherit Foo's properties any more. if you attach a new
property to Foo.prototype, all existing and future instances of Foo
will inherit it from then on. however, if you replace Foo.prototype
with another object, it won't affect previous instances of Foo, since
their __proto__ still points to the original Foo.prototype object
(i.e. new Foo() will return instances with different behaviours
depending on when you instantiate them!). You could also inject a new
class into the inheritance chain, e.g.:
var iLikeToConfuseMyCoworkers = { secretProp: "bet you'll never
manage to debug this" };
iLikeToConfuseMyCoworkers.__proto__ = Bar.prototype;
Foo.prototype.__proto__ = iLikeToConfuseMyCoworkers;
so, Johannes' suggestion was to simply
myClip.__proto__ = MyClass.prototype;
it's good to know that kind of stuff, imho, because you get some
insight what happens in the background. to use that knowledge,
however, almost always isn't the right thing to do, especially if
others will have to work with your code.
hth,
mark
--
http://snafoo.org/
jabber: [EMAIL PROTECTED]
_______________________________________________
osflash mailing list
[email protected]
http://osflash.org/mailman/listinfo/osflash_osflash.org