AS3 和 Haxe 语法对比

2014年03月29日 17:50GMT+8

本文档的目的是帮助 熟悉 Actionscript 3 的开发人员快速入门 Haxe。更多 Haxe 特定文档请参阅主要的 Haxe.org.

这个文档源文来自: Haxeflixel’s guide.

Basic Types(基础类型)

AS3

Boolean
int
Number
Object
void
Array
Vector.<String>

Haxe

Bool
Int
Float
Dynamic
Void
Array<Dynamic>
Array<String>

Const (常量)

AS3

const MAX:int = 100;

Haxe

static inline var MAX:Int = 100;
// haxe 的常量只允许为 Int, Bool, Float, 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(var value:String in items) {

}

for (var propertyName:String in object) {

}

Haxe

// haxe 没有 for(var i=0; i<10; i++)

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

// haxe 中的 switch 不需要 break;

switch (value) {

   case 1:
      trace ("Equal to 1");

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

}
        

Type Inference(类型推断)

AS3

var hi = "Hello World";

// 类型为 Object,不能通过严格模式的编译

Haxe

var hi = "Hello World";

// 类型为 String,能正常通过编译

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

import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;

if (vehicle is Car) {
}

name = getQualifiedClassName (vehicle);
type = Class (getDefinitionByName (name);

Haxe

// haxe 中没有 is,as 以及 instanceof 这些关键字


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

var type2:Dynamic = hello;

Getters and Setters

AS3

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

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

Haxe

var _x:Float;
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 ():Number {
   return _x;
}

Haxe

public var x (default, null):Float;
// setter 的值可以为: null, never 或 set

// null 表示仅能在类的内部修改其它

// never 表示不可修改

延时

AS3

var cancelID:uint = setTimeout(callb, 1000);

clearTimeout(cancelID);// 取消

Haxe

var t = haxe.Timer.delay(callb, 1000);

t.stop(); // 取消

类实例对比

AS3

// AS 中对比二个实例是否为一个很简单

trace(this == root);

//或使用全等号

trace(this === root);

Haxe

// Haxe 则需要类型一致, 或者互为父,子类

trace(this == cast(root,Sprite)); //


//或使用 cast

trace(this == cast root);

条件编译

AS3

// 需要 IDE 支持

CONFIG::DEBUG{
  trace("debug");
}

Haxe

#if debug
  trace("debug");
#end

更多特性

Haxe 增加了一些 Actionscript 3 没有的语法特性:

个人笔记