Cheat Sheet

This document aims to give developers familiar with JavaScript a quickstart to Haxe. For more Haxe specific documentation please refer to the main Haxe.org website:

Additional Features

Haxe includes support for:


Haxe & JavaScript common syntax comparison

This guide based off from OpenFL's guide and HaxeFlixel'd guide.


Package Declarations

JavaScript

CommonJS vs AMD vs ES2015 (preferred) module

// CommonJS
var package = require("package");

// AMD
require(["package"], function(package) {
  package.sayHello();
});

// ES2015 (ES6)
import package from "package";

Haxe

package com.example.myapplication;

Defining a Class

JavaScript

Prototypal inheritance

// ES5
function MyClass() {}

MyClass.prototype.someFunction = function() {};

function NewClass() {
  MyClass.apply(this, arguments);
}

NewClass.prototype = Object.create(MyClass.prototype);

NewClass.prototype.changedSomeFunction = function() {
  MyClass.prototype.someFunction.apply(this);
};

// ES2015 (ES6) (syntactic sugar)
class MyClass {
  someFunction() {}
}

class NewClass extends MyClass {
  changedSomeFunction() {
    super.someFunction();
  }
}

Haxe

classical inheritance

class MyClass {

    public function new () {

    }

}

Loops

JavaScript

// (classic C-style for-loop)
for (i = 0; i < 100; i++) {}

for (propertyName in object) {
}

for (variable of iterableObject) {
}

do {} while (i < 5);

while (i < 5) {}

Haxe

// (iterator based for-loop)
for (i in 0...100) {

}

for (value in items) {

}

var fields = Reflect.fields (object);
for (propertyName in fields) {

}

do {

} while (i < 5)

while (i < 5) {

}

Logging

JavaScript

console.log("hello world");

Haxe

trace("hello world");

Switch Statements

JavaScript https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch

switch (value) {
  case 1:
    console.log("Equal to 1");
    break;
  default:
    console.log("Not equal to 1");
}

Haxe

switch (value)
{
   case 1:
      trace ("Equal to 1");

   default:
      trace ("Not equal to 1");
}

Type Inference

JavaScript

Dynamic typing

var hi = "Hello World";
console.log(typeof hi);
// type is "String"
hi = 1;
console.log(typeof hi);
// type is "Number"

Haxe

Static typing

var hi = "Hello World";
$type (hi); // Warning : String
// type is String
// even works for code completion
hi = 1; // Int should be String
// will not compile

Type Casting

JavaScript

there is no type casting in js

var toString = String(10);
var toNumber = Number("10");
var toInteger = parseInt(10.1);

Haxe

var car:Car = cast vehicle;

// or for a safe cast:

var car = cast (vehicle, Car);

var toString = Std.string (10);
var toNumber = Std.parseFloat ("10");
var toInteger = Std.int (10.1);

Checking for Null

JavaScript

if (object == null) {
}

if (!object) {
}

Haxe

if (object == null) {

}

Hash Tables

JavaScript

var table = new Object(); // or just {}
table["key"] = 100;

console.log(table.hasOwnProperty("key"));

for (var key in table) {
  console.log(key + " = " + table[key]);
}

delete table["key"];

Haxe

var table = new Map<String, Int> ();
table.set ("key", 100);

trace (table.exists ("key"));

for (key in table.keys ())
{
    trace (key + " = " + table.get (key));
}

table.remove ("key");

Rest Parameters

JavaScript

https://developer.mozilla.org/nl/docs/Web/JavaScript/Reference/Functions/rest_parameters

function test(...params) {}

test(1, 2, 3);

Haxe

function test (params:Array<Dynamic>) {

}

Reflect.makeVarArgs (test) (1, 2, 3);

Function Types

JavaScript

function hello(msg) {}

var type = hello;

Haxe

function hello (msg:String):Void {

}

var type:String->Void = hello;

// can also use Dynamic

Getters and Setters

JavaScript

var lost = {
  loc: "Island",
  get location() {
    return this.loc;
  },
  set location(val) {
    this.loc = val;
  }
};
console.log(lost.location); // will return "Island"
lost.location = "Another island";
console.log(lost.location); // will now return "Another island"

Haxe

public var location(get_location, set_location):String;
private var _location:String = "Island";

function get_location():String {
    return _location;
}
function set_location(value:String):String {
    return _location = value;
}

....

trace(location); // will return "Island"
location = "Another island";
trace(location); // will now return "Another island"

results matching ""

    No results matching ""