GWT - How to call a JavaScript function inside another JavaScript function

时间:2016-04-04 18:21:41

标签: javascript gwt jsni

I have a GWT class with two JSNI JavaScript functions. I want to call one function inside another, like this:

public class MyClass
{
  public native void Function1()
  /*-{
    console.log("Function 1");
  }-*/;

  public native void Function2()
  /*-{
    Function1();
  }-*/;
}

In this case, when executing Function2 i'm getting "Function1 is not defined".

Keep in mind that I don't want to embeed Function1 inside Function2 code, I need two different JSNI functions (so I can call Function1 from multiple functions).

Thank you.

2 个答案:

答案 0 :(得分:1)

将两种方法也改为static 你可以简单地从你的jsni代码中调用任何方法:

public class MyClass
{
  public static native void Function1()
  /*-{
    console.log("Function 1");
  }-*/;

  public static native void Function2()
  /*-{
    @xxx.xxxxx.xxxx.MyClass::Function1();
  }-*/;
}

可以找到更多信息here

答案 1 :(得分:1)

package mypackage;  

public class Account {
    private int balance = 0;

 public int add(int amt) {
      balance += amt;
   }

public native void exportAdd() /*-{
    var that = this;
    that.@mypackage.Account::add(I)(10);
}-*/;
}