A File object is not connected to an output context. A File has a "transform" method. The transform method returns a MediaTransformOutput object. This object only has a toHtml(), the parser directly calls toHTML on image objects from inside the parser (or actually linker). The FilePage object does something similar for the File history, as do a few Special pages.
Currently MediaHandlers that need JS, add these modules using the method parserTransformHook, which one could either directly add modules to the $parser object, or add an OuputHook, to mess with $wgOut later on. Special pages do this on title matching in the beforedisplay hook.
This is not that good an interface. Besides being rather indirect way to add resource loader modules, its (almost) impossible to trigger this from a special page where we call transform directly, and don't have a $parser object that is parsing something.
I'm not sure what a better interface would be. My first thought would be that responsibility for this should not be in MediaHandler, but instead be in the MediaTransformObject. The mto could at least have a method getModules() which special pages could call, or perhaps something like $mto->addModulesToOutput( $this->getOutput() ); Still less then ideal since people will be bound to forget to call it as most media types don't need js, but still would be much better than current situation.