2009-12-09 40 views
1

即时通讯发送命令字符串到Flash应用程序,执行调试命令。通过Firebugs控制台/命令行来做到这一点看起来是最简单的方法来启动和运行。添加全局JavaScript函数,jQuery风格?

目前我可以使用ExternalInterface调用控制台从Flash应用程序登录到Firebug控制台。还可以通过调用ExternalInterface.addCallback方法添加的command方法发送Flash应用程序命令。因此,在包含Flash应用程序的HTML文件,目前我有一些JavaScript:

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

,并在命令行萤火我可以输入:

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

和Flash应用程序接收到这一点。所以这一切都很好,但我想尽可能缩短app.command的通话时间。

所以,我想分配app.command功能的单个字符函数风格的jqueries $方法。那么我将如何去实现一个函数$$$?

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

回答

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

那么这是惊人的容易:) – 2009-12-09 09:48:58

+0

是的,我知道。 :) – 2009-12-09 09:50:15

+0

请注意,使用var时,函数将不会是全局的,并且可以在任何地方访问。 – googletorp 2009-12-09 09:51:00

0

实施例:

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

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

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

这相当于:

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

到明确的函数声明。

JavaScript有所谓first class functions

2

你不必做这个复杂和使用闭等如jQuery,你可以做

$$$ = app.command 

或包裹它的功能

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

内或将其连接到窗前,仿佛jQuery:

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