AS3 and Haxe Comparison

来源:互联网 发布:java项目打成war包 编辑:程序博客网 时间:2024/05/01 05:51

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

  • Haxe Syntax
  • Haxe Language Reference

Additional Features

In addition to most of the features of Actionscript 3, Haxe includes support for:

  • enums
  • type parameters
  • structures
  • custom iterators
  • conditional compilation
  • inlining
  • and more!

Haxe & AS3 common syntax comparison

This guide is based off from OpenFL's guide.

Basic Types

AS3
BooleanintNumberObjectvoidArrayVector.<String>

Haxe

BoolIntFloatDynamicVoidArray<Dynamic>Array<String>
 


Package Declarations

AS3

package com.example.myapplication {}

Haxe

package com.example.myapplication;
 


Defining a Class

AS3

public class MyClass {   public function MyClass () {   }}

Haxe

class MyClass {   public function new () {   }}
 


Loops

AS3

for (var i:uint = 0; i < 100; i++) {}for each (var value:String in items) {}for (var propertyName:String in object) {}        

Haxe

for (i in 0...100) {}for (value in items) {}var fields = Reflect.fields (object);for (propertyName in fields) {}        
 


Switch Statements

AS3

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

Haxe

switch (value) {   case 1:      trace ("Equal to 1");   default:      trace ("Not equal to 1");}        
 


Type Inference

AS3

var hi = "Hello World";// type is Object// fails to compile in strict mode        

Haxe

var hi = "Hello World";// type is String// even works for code completion        
 


Type Casting

AS3

var car:Car = vehicle as Car;var toString:String = String (10);var toNumber:Number = Number ("10");var toInteger:int = int (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);        
 


Type Details

AS3

if (vehicle is Car) {}import flash.utils.getDefinitionByName;import flash.utils.getQualifiedClassName;name = getQualifiedClassName (vehicle);type = Class (getDefinitionByName (name);        

Haxe

if (Std.is (vehicle, Car)) {}type = Type.getClass (vehicle);name = Type.getClassName (type);        
 


Checking for Null

AS3

if (object == null) {}if (!object) {}        

Haxe

if (object == null) {}        
 


Hash Tables

AS3

var table:Object = new Object ();table["key"] = 100;trace (table.hasOwnProperty ("key"));for (var key:Object in table) {   trace (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

AS3

function test (...params):void {}test (1, 2, 3);        

Haxe

function test (params:Array<Dynamic>) {}Reflect.makeVarArgs (test) (1, 2, 3);
 


Reflection

AS3

var foo = object["foo"];bar.apply (this, [ "hi" ]);        

Haxe

var foo = Reflect.field (object, "foo");Reflect.callMethod (this, bar, [ "hi" ]);        
 


Function Types

AS3

function hello (msg:String):void {}var type:Function = hello;        

Haxe

function hello (msg:String):Void {}var type:String->Void = hello;// can also use Dynamic        
 


Getters and Setters

AS3

function get x ():Number {   return _x;}function set x (value:Number):void {   _x = value;}        

Haxe

public var x (get, set):Float;function get_x():Float {   return _x;}function set_x(value:Float):Float {   return _x = value;}        
 


Read-Only Properties

AS3

function get x ():Float {   return _x;}        

Haxe

public var x (default, null):Float;// null allows private access// never would restrict all access        
 
from: http://haxeflixel.com/documentation/as3-and-haxe-comparison/
0 0
原创粉丝点击