Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static property not seen in Firefox

Tags:

haxe

In my haxe project, I am targeting javascript, and exposing class using @:expose to call it from outside haxe project.

In my Main class, I am using instance to access the single tone class.

like:

com.Main.instance

now, when I try to access a function ini inside the class, it will work only when using Chrome, but it will get an error at Firefox:

TypeError: com.Main.instance is undefined

Any idea why it has worked on Chrome, but not in Firefox?

I am using haxe version 3.4.0

Updated i added minimized haxe sample file to reproduce the problem

package com;

import js.Browser;
@:expose
class Main {
    /*
    Using this var results in undefined
    example at Firefox console:
    >> com.Main.instance
    undefined
    */        
    @isVar public static var instance(get, null):Main = null;

    static function get_instance():Main {
        if (instance == null)
            instance = new Main();
        return instance;
    }

    function new() {
    }
    static function main() {
        trace('Hello World');
    }
    /*
    Calling this method results in error
    example at Firefox console:
    >> com.Main.instance.init();
    TypeError: com.Main.instance is undefined
    */
    public function init(){
        Browser.console.log("Main Initialized");
    }
}

Here is the HTML page:

<!DOCTYPE html>
<html>
<head>
    <script
  src="https://code.jquery.com/jquery-3.2.1.min.js"
  integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
  crossorigin="anonymous"></script>

    <script type="text/javascript" src="map.js"></script>
</head>


<body >

     <script>


$(document).ready(function () {
   com.Main.instance.init();
});

        </script>




</body>
</html>

And here is the compiled map.js:

// Generated by Haxe 3.4.0
(function ($hx_exports) { "use strict";
$hx_exports["com"] = $hx_exports["com"] || {};
var com_Main = $hx_exports["com"]["Main"] = function() {
};
com_Main.get_instance = function() {
    if(com_Main.instance == null) {
        com_Main.instance = new com_Main();
    }
    return com_Main.instance;
};
com_Main.main = function() {
    console.log("Hello World");
};
com_Main.init = function() {
    window.console.log("Main Initialized");
};
com_Main.__meta__ = { statics : { instance : { isVar : null}}};
com_Main.main();
})(typeof exports != "undefined" ? exports : typeof window != "undefined" ? window : typeof self != "undefined" ? self : this);

//# sourceMappingURL=map.js.map
like image 999
suhaibshakra Avatar asked Nov 07 '22 20:11

suhaibshakra


1 Answers

The use of Haxe properties from external JS code isn't really supported, as far as I know (see issue #2469).

You can call the getter directly (e.g. com.Main.get_instance().init()) or check that issue for other solutions.


Note: I couldn't confirm that the example works in Chrome (59 or 60) and, really, I don't think that it could.

like image 109
Jonas Malaco Avatar answered Dec 09 '22 01:12

Jonas Malaco