COMMUNITY

JS Interop: exposing methods that accept a map of options

I want to expose a class (to other JS code, which may not be written in Haxe) and I want the constructor to accept an options object. So, the external JS code might look something like this:

chessGame = new ChessGame({
    "difficulty": 100,
    "timeLimit": 60
});

In my Haxe code, I’ve exposed the class and tried defining the constructor like this:

public function new(options:Map<String, Int>) {
    trace(options); // Also tried options.toString()
}

The result of the trace is an empty object. What am I missing, or, how else could I approach this?

tl;dr → want to expose a class to be used by external JS code, and I want the constructor (and potentially other methods) to accept an object/map of settings/config data.

That doesn’t seem like a good use case for a map, a properly typed structure would be ideal:

typedef Options = {
    var difficulty:Int;
    var timeLimit:Int;
}

Otherwise haxe.DynamicAccess would also work.

Perfect—a typedef is exactly what I needed. Thank you :slight_smile:

Just to clarify the terminology: the thing you needed is an anonymous object/structure, typedef just gives the type an alias.