Skip to content

Instantly share code, notes, and snippets.

@creynders
Created October 26, 2011 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save creynders/1a6da0c8b1d5322940fe to your computer and use it in GitHub Desktop.
Save creynders/1a6da0c8b1d5322940fe to your computer and use it in GitHub Desktop.
Reference instance method from class object
//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.
@Stray
Copy link

Stray commented Oct 26, 2011

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 );

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

@tschneidereit
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment