简体   繁体   English

如何在 Haxe 中显式声明变量类型为 object?

[英]How do I explicitly declare the type of the variable as object in Haxe?

If I have a variable called str that is a String, I can do the following如果我有一个名为str的变量,它是一个字符串,我可以执行以下操作

var str:String = "value";

However, I don't know what kind of type I need to use for a generic object like this但是,我不知道像这样的通用 object 需要使用哪种类型

var myObj:???? = {
  key1: value1,
  key2: value2
}

I know I don't need to declare it but I still want to know what the proper text is in place of????我知道我不需要声明它,但我仍然想知道正确的文本是什么????

Type.typeof() says it's a TObject but I can't use that. Type.typeof()说它是一个TObject但我不能使用它。

it depends on key types but you might want something like this:这取决于密钥类型,但您可能想要这样的东西:

    var myObj:{key1:String, key2:String} = {
      key1: "foo",
      key2: "bar"
    };

If you plan on reusing this structure you should look at defining a Class or Typedef .如果您计划重用此结构,您应该考虑定义 Class 或Typedef Otherwise, you don't necessarily have to provide a type.否则,您不必提供类型。 You could just do the following and let the compiler assign its own type:您可以执行以下操作并让编译器分配自己的类型:

var myObj = {
      key1: "foo",
      key2: "bar"
    };

If using a typedef you would do like:如果使用 typedef 你会喜欢:

typedef FooBar = {
  var foo : String;
  var bar : String;
}
var myObj:FooBar = {
  foo: "foo",
  bar: "bar"
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM