O'Reilly logo

haXe 2 by Benjamin Dasnois

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action—Writing an extern

Imagine that we are writing a User class in JavaScript:

function User()
{
var name;
var age;
}
User.prototype.outputInfo = function()
{
var el = document.createElement("div");
el.innerHTML = this.name+"("+ this.age+")";
document.body.appendChild(el);
}

Now, if we want to use this class from our haXe application, then we have to write the corresponding extern class as follows:

extern class User
{
public var name:String;
public var age:Int;
public function outputInfo():Void;
public function new():Void;
}

There are several things that you should note:

  • You have to prefix your class declaration with the extern keyword
  • We do not write any code inside function declaration
  • Constructors should be declared returning Void

What ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required