12.2 JavaScript

Haxe provides the ability to target JavaScript. It does so by transpiling Haxe to JavaScript. The current implementation targets ECMAScript 5. But it is possible to target lower versions using -D js-es=<value>.

When choosing the JavaScript target, only the used Haxe code of the project (and used parts of the standard library) are transpiled to JavaScript. This results in optimal filesize which is also readable. Source maps are available and there are several ways to get debug information. The standard library aims to have same functionality across all targets.

Usage

You may want to compile Haxe to JavaScript in the following scenarios:

Client-side JavaScript Interacting with DOM elements. Haxe provides up-to-date typed interfaces to interact with the Document Object Model, allowing creation and update of DOM elements.

Haxe can be used together with existing third-party libraries and frameworks, such as jQuery, React or Vue. To access third-party frameworks with a strongly-typed API, there are extern libraries available on Haxelib. Alternatively, it is possible to create own externs (see Using external JavaScript libraries) or use the dynamic type to access any framework, see Accessing Target-specific Syntax.

Creating graphics using Canvas and WebGL Use Haxe to create graphical elements on a web page using WebGL.

Libraries like OpenFL, Heaps and Kha make use of WebGL as one of their backends.

Creating Haxe code that targets server-side JavaScript Working with server-side technology. Haxe can be used to create server-side JavaScript like Node.js.

12.2.1: Getting started with Haxe/JavaScript

12.2.2: ECMAScript 6 class generation

12.2.3: Using external JavaScript libraries

12.2.4: JavaScript target Metadata

12.2.5: Exposing Haxe classes for JavaScript

12.2.6: Loading extern classes using "require" function