开发者

Adding a global Javascript function, jQuery style?

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 f开发者_StackOverflow社区unction to a single character function in the style of jqueries $ method. So how would I go about implementing a function $$$?

$$$('screen/ruler.show')


$$$ = function( args ) {
    app.command( args );
}


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜