secret
Created

Reference instance method from class object

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//aim is to be able to register listeners for lazily instantiated objects
//so I need to be able to refer to an instance method, w/o having an instance of that class (yet)
//the idea is that when the signal is fired, an instance of the class is created and the method is called
 
//for example:
//I have a SignalClassMapDemo class with a non-static showStream method, which needs to be called when
//the startupCompleteSignal is dispatched
 
//the following doesn't work, unfortunately
signalClassMap.mapSignal( startupCompletedSignal, SignalClassMapDemo, SignalClassMapDemo.prototype.showStream )
 
//and I REALLY don't want to do this
signalClassMap.mapSignal( startupCompletedSignal, SignalClassMapDemo, "showStream" )
 
//It's like joel's signalCommandMap, but instead of always calling an "execute" method, I want to be able to
//define what method should be called.

Sadly I think this isn't possible, without - as you say - the "showStream" stringiness.

2 options I can think of -

1) Static properties on the SignalClassMapDemo that give the name of the function as a string constant so you'd get

signalClassMap.mapSignal( startupCompletedSignal, SignalClassMapDemo, SignalClassMapDemo.SHOW_STREAM );

2) Metadata to mark up the handler (but then really you're back using strings with no compile safety)

That's not possible, unfortunately.

In AS3, the prototype lives a sad life, hiding in the shadows where it can almost never be seen. Only if some unsuspecting tourist happens upon its cave, perhaps by going "gee, I wonder what happens if I 'toString' this object that doesn't have a toString method itself", it comes out of hiding and says "uh uh, I know that one!". And then, sadly, it doesn't. Not really, anyway. All it comes up with is some incomprehensible garbage along the lines of "[object Object]".

Meaning: Almost all of the builtin objects' methods and properties live in the automatically opened namespace "AS3" and not on the prototype of any object at all. And since you can't inspect a class's members table without reflection, you also can't directly point to the instance members without having an instance.

Oh, and also, whenever you refer to a method of an instance, you're not really refering to the method itself. Instead, you can only ever get to the implicitly created "MethodClosure", which contains both the function and the scope to invoke it in - in this case: the instance. Internally, whenever you do something like this:

var callback : Function = myInstance.callback;
callback(some, args);

what happens behind the scenes is the equivalent of

callback.apply(myInstance, arguments);

Because of that, it wouldn't even make sense for the methods to be exposed in the way you want them to be. All you could ever get would be unbound functions, not related to any instance whatsoever.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.