需求:外部定义了一个test.js的文件,我想通过haxe调用里面的函数mytest()
准备文件test.js:
[sourcecode]
Test.prototype.mytest = function() {
alert("hello,amyflash");
}
function Test() {}
[/sourcecode]
对应test.hx:
[sourcecode]
package ;
/**
* ...
* @author amyflash.com
*/
extern class Test
{
public function new():Void;
public function mytest():Void;
}
[/sourcecode]
网页里加入test.js的引用,index.html:
[sourcecode]
[/sourcecode]
Main.hx:
[sourcecode]
package ;
import js.JQuery;
import js.Lib;
/**
* ...
* @author amyflash.com
*/
class Main
{
static function main()
{
new JQuery(null).ready
(
function(e:JqEvent)
{
var domElem = new JQuery("button");
var clickHandlerFunction = function (e)
{
var t = new Test();
t.mytest();
}
domElem.bind( "click", clickHandlerFunction );
}
);
}
}
[/sourcecode]
编译Main.hx,生成myjquery.js,启动haxe服务器(如何启动,参考这篇文章),打开网址
http://https://https://amyflash.com:2000/index.html
点击按钮,出现alert框,显示hello,amyflash,大功告成!