cincheo / jsweet

A Java to JavaScript transpiler.
http://www.jsweet.org
Other
1.46k stars 160 forks source link

Defining function #115

Closed WhiteTrashLord closed 8 years ago

WhiteTrashLord commented 8 years ago

Maybe I will replace GWT with Jsweet because I only need Javascript functionality.

Is it already possible to define functions in Jsweet which can be also invoked in external / own Javascript code? Sorry I couldn't find anything like this.

renaudpawlak commented 8 years ago

Sure. JSweet has really low impedance with JavaScript (don't hesitate to look at the JS output code). In order to define functions, just define static methods in Globals classes. For example

package p1.p2;

public class Globals {
   public static void myFunction() { ... }
}

Will be invocable from JavaScript: p1.p2.myFunction().

You can also instantiate classes and call methods directly.

package p1.p2;

public class MyClass {
   public void myMethod() { ... }
}

Will be invocable from JavaScript: new p1.p2.myClass().myMethod().

If you want to tune the generated code, you can also take a look at the @Root annotation to omit packages in the output code.