我想要获得动态类型的参数。 我有关于泛型的知识,但我没有找到有关 javascript 动态类型的信息。 我希望这里任何人都可以帮助我。谢谢。
public function addItem (item) : boolean {
if (!item || !item.weight) return;
if (item.weight > item.weight + inventoryWeight) return false;
items.push(item);
inventoryWeight += item.weight;
return true;
}
我需要将这段代码转换为js
public class SomeClass
{
//Here is a generic method. Notice the generic
//type 'T'. This 'T' will be replaced at runtime
//with an actual type.
public T GenericMethod<T>(T param)
{
return param;
}
}
最佳答案
but I don't find there information about javascript dynamic type
Javascript 不允许泛型。您无法在 Unity 中使用 Javascript 执行此操作。您只能将泛型与 Unity 支持的 C# 和 Boo 一起使用。如果您想使用泛型,请从 Javascript 转换为 C#。
Here是 C# 和 Boo 的泛型教程。单击 Javascript 选项卡时没有示例代码。如果您决定转换到 C#,请单击 here有关 C# 的官方 Unity 教程。
关于javascript - JavaScript 中的 Unity 动态参数/参数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39812844/