AS2 vs __resolve
Posted January 17, 2004 by senocular
Recently I was working on a class in AS2 that would help in serving a sort of multiple inheritance for Flash, at least on a basic level. I've already done this for AS1, so I thought it was time for an upgrade. In my attempts, however, something struck me. It seems that access to class members in AS2 is given through a kind of local scope as though defined all within a function call. Because of this, using __resolve to capture non-native member referencing (to return references available from alternative supers) just won't work.
With the AS1 method of "multiple inheritance", I used __resolve to catch method and property calls from an instance that weren't available in that instance's own class or its real superclass. Then, the additional super classes added were accessed and the appropriate member was returned when matched. You would think something similar could be achieved with AS2, but that is not fully the case.
The reason for lack of __resolve's full effectiveness in AS2 is how AS2 allows referencing of members without the need of a 'this' prefix from within the class constructor and its methods. Because of that, the object instance itself is not technically referenced directly and so a __resolve on unknown references is never initiated (a __resolve in the timeline of the origination call will capture the reference however).
It appears that such class members are available instead not only within the object instance but also through a kind of local scope much in the way you might expect the class to behave given its own syntax. For example, a function declared within a function can access local variables in the parent function's scope:
In this situation, however, if value was instead a non-existant variable, a __resolve call would not go to a __resolve within the local scope of the parent function, but rather, through to the timeline in which it exists.
And this is the case with AS2 classes. Any __resolve within those classes will only function for calls directly off the instance or those within the class being referenced through the 'this' keyword. As such, any __resolve used in an attempt to capture all member references from classes would have to be defined in the timeline - not a convenient task for this multiple inheritance trickery. And so ends my quest - at least for now.
The reason for lack of __resolve's full effectiveness in AS2 is how AS2 allows referencing of members without the need of a 'this' prefix from within the class constructor and its methods. Because of that, the object instance itself is not technically referenced directly and so a __resolve on unknown references is never initiated (a __resolve in the timeline of the origination call will capture the reference however).
It appears that such class members are available instead not only within the object instance but also through a kind of local scope much in the way you might expect the class to behave given its own syntax. For example, a function declared within a function can access local variables in the parent function's scope:
parent = function(){
var value = 2;
child = function(){
trace(value); // traces 2
}
child();
}
parent();In this situation, however, if value was instead a non-existant variable, a __resolve call would not go to a __resolve within the local scope of the parent function, but rather, through to the timeline in which it exists.
parent = function(){
var value = 2;
child = function(){
trace(novalue);
}
child();
}
__resolve = function(v){
trace(v); // traces "novalue"
}
parent();And this is the case with AS2 classes. Any __resolve within those classes will only function for calls directly off the instance or those within the class being referenced through the 'this' keyword. As such, any __resolve used in an attempt to capture all member references from classes would have to be defined in the timeline - not a convenient task for this multiple inheritance trickery. And so ends my quest - at least for now.