COMMUNITY

Attaching new variable to global window object (js.Browser.window)

Hello people, this is my first day using Haxe, and I am liking it so far but I have a question :slight_smile:

I am using js.Browser and for debugging I want to attach an object to the global window object.

However the following (understandably) fails with js.html.Window has no field foo:

Browser.window.foo = 33;

Which of course makes sense … Haxe has a strict type system. I’d however still like some escape hatch for this. I’m very new to Haxe so I don’t know if there is one?

(Browser.window:Dynamic).foo = 33;
But try to avoid such hacks in your production code.

4 Likes

Another approach:

@:native("window")
extern class Globals {
  static var foo:Bar;
}
3 Likes

Hi, not answering your specific question, but you can use @:expose to make Haxe classes or static fields accessible from Javascript. Maybe it can help your debugging ?

class Test {
  static function main() {
 
   var window:haxe.DynamicAccess<Dynamic> = untyped js.Browser.window;
  
    window.set('gina','pino');
    trace(window.get('gina'));
    
  }
}```
1 Like