tags:

views:

97

answers:

3

Im looking to send command strings to a flash app, to execute debug commands. Doing this through Firebugs Console/Command-line looks like the simplest way to get this up and running.

At the moment I can log to the firebug console from the flash app by calling out to console using the ExternalInterface. And also send the flash app commands by calling a command method added by the ExternalInterface.addCallback method. So at the moment in the html file that contains the flash app I have some Javascript:

$(document).ready(function(){
    app = $("#${application}").get(0)
})

and at the firebug commandline I can type:

app.command('screen.ruler.show')

And the flash app receives this. So this is all fine, but I would like to make the app.command call as short as possible.

So I would like assign the app.command function to a single character function in the style of jqueries $ method. So how would I go about implementing a function $$$?

$$$('screen/ruler.show')
+3  A: 
$$$ = function( args ) {
    app.command( args );
}
Jacob Relkin
Well that was surprisingly easy :)
Brian Heylin
Yeah, i know. :)
Jacob Relkin
Note, when using var the function wont be global and accessible every where.
googletorp
googletorp: It depends on where your `var` is used. In global (window) context it will effectively create "global" function.
Kuroki Kaze
if you don't use `var` inside a function, then it will be declared in global scope.
Jacob Relkin
A: 

Example:

$(function() {
  $$$("Fred");
});

var app = {
  command: function(arg) {
    alert("hello " + arg);
  }
};

function $$$(arg) {
  app.command(arg);
}

This is equivalent:

var $$$ = function(arg) {
  app.command(arg);
}

to the explicit function declaration.

Javascript has what are called first class functions.

cletus
+2  A: 

You don't have to make this complex and use closures etc like jQuery, you can just do

$$$ = app.command

or wrap it inside a function

function $$$(arg) {
    app.command(arg);
}

or attach it to window like jQuery:

window.$$$ = function(arg) {
    app.command(arg);
}
googletorp