# How do I create a Haxe class that calls into my own local javascript file?

**URL:** <https://community.haxe.org/t/how-do-i-create-a-haxe-class-that-calls-into-my-own-local-javascript-file/2445>\
**Category:** Haxe\
**Tags:** macro, haxe-js\
**Created:** [May 16, 2020, 11:50pm UTC](https://community.haxe.org/t/how-do-i-create-a-haxe-class-that-calls-into-my-own-local-javascript-file/2445 "2020-05-16T23:50:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tanz0rz](https://avatars.discourse-cdn.com/v2/letter/t/bc79bd/32.png) [@Tanz0rz](https://community.haxe.org/u/Tanz0rz)\
**Post date:** [May 16, 2020, 11:50pm UTC](https://community.haxe.org/t/how-do-i-create-a-haxe-class-that-calls-into-my-own-local-javascript-file/2445/1 "2020-05-16T23:50:41Z")

</div>

I want to write a class that my Haxe code can call to execute raw javascript. I read through [this guide](https://haxe.org/manual/target-javascript-external-libraries.html), but do not understand it enough to make progress.

Here is some pseudo-code to roughly outline what I want to do:

`Haxe_translation_class.hx`:

```haxe
#if html5
extern class HaxeGeneralClass {
    @:overload(function(my_javascript_library.init):Void { })
    public static function init():Void;
}
#end

```

`My_javascript_library.js`:

```haxe
static init() {
    console.log("javascript version of library initialized");
}

```

---

<div class="post-metadata">

**Author:** ![basro](https://community.haxe.org/user_avatar/community.haxe.org/basro/32/738_2.png) [@basro](https://community.haxe.org/u/basro)\
**Post date:** [May 17, 2020, 2:14am UTC](https://community.haxe.org/t/how-do-i-create-a-haxe-class-that-calls-into-my-own-local-javascript-file/2445/2 "2020-05-17T02:14:01Z")

</div>

@:overload metadata is used for when a function can have multiple different sets of arguments.

In your case I think you are looking for the @:native metadata.

For example:

```haxe
#if html5
@:native("my_javascript_library")
extern class HaxeGeneralClass {
    public static function init():Void;
}
#end

```

Calling `HaxeGeneralClass.init();` will generate the javascript code: `my_javascript_library.init();`

---

<div class="post-metadata">

**Author:** ![Tanz0rz](https://avatars.discourse-cdn.com/v2/letter/t/bc79bd/32.png) [@Tanz0rz](https://community.haxe.org/u/Tanz0rz)\
**Post date:** [May 22, 2020, 11:45pm UTC](https://community.haxe.org/t/how-do-i-create-a-haxe-class-that-calls-into-my-own-local-javascript-file/2445/3 "2020-05-22T23:45:56Z")

</div>

I just saw this! I didn’t get a notification that someone replied. I think you are right about the native tag, but I have no clue how to get my\_javascript\_library in scope when the game is actually running. I build my game using lime and it is a haxeflixel game.

---

<div class="post-metadata">

**Author:** ![basro](https://community.haxe.org/user_avatar/community.haxe.org/basro/32/738_2.png) [@basro](https://community.haxe.org/u/basro)\
**Post date:** [May 23, 2020, 12:18am UTC](https://community.haxe.org/t/how-do-i-create-a-haxe-class-that-calls-into-my-own-local-javascript-file/2445/4 "2020-05-23T00:18:58Z")

</div>

What library are you trying to use?  
I’m not familiar with haxeflixel or lime but It’s often enough to add `<script src="url/to/the/library.js"></script>` before your game’s main script tag.

---

<div class="post-metadata">

**Author:** ![Tanz0rz](https://avatars.discourse-cdn.com/v2/letter/t/bc79bd/32.png) [@Tanz0rz](https://community.haxe.org/u/Tanz0rz)\
**Post date:** [May 23, 2020, 1:33am UTC](https://community.haxe.org/t/how-do-i-create-a-haxe-class-that-calls-into-my-own-local-javascript-file/2445/5 "2020-05-23T01:33:51Z")

</div>

I got it figured out. your solution got me halfway there, so thank you! Haxeflixel seems to have a unique flow. I will outline the steps to my solution here:

1. In your `Project.xml` file, add

```haxe
<dependency path="path/to/file.js" if="html5"/>

```

1. Create an extern class in your project that has the same functions and arguments as the javascript class you want to import. The extern class can be named anything, but the `@:native` native tag must match the javascript class name.

```haxe
@:native("class_name_to_import")
extern class MyHaxeExternClass {
    public static function function_from_javascript_class():Void;
}

```

If you want to create a library that calls out to the json class, add the `<dependency>` tag to an `include.xml` instead and the contents of that will be appended to the `Project.xml` of whatever game imports it.
