Haxe Javascript:在不使用 Reflect 的情况下通过其名称获取和设置 属性
Haxe Javascript: get and set property by its name without using Reflect
我希望能够在不使用 Reflect API 的情况下通过名称设置和获取 属性 值。
我正在制作某种补间引擎,为数百个对象设置动画,并且对函数 Reflect.getProperty
和 setProperty
的调用花费了相当多的 CPU 时间。
有没有办法绕过 Reflect API 而不必在我的 Haxe 代码中注入原始 javascript
? (所以在js之外的其他平台也可以使用)
所以不要这样做:
finalValue = Reflect.getProperty(object, propertyName);
我希望能够做到这一点:
finalValue = object[propertyName];
谢谢。
去untyped
怎么样?
// set
untyped object[propertyName] = 15;
// get
var value = untyped object[propertyName];
亲自尝试一下:http://try.haxe.org/#5b61e
Warn Going untyped lets you omit the type system and is more error prone. If you use instances with Haxe properties (getter/setters) it might give unexpected results. That's why using Reflect
is considered safer.
将 "untyped" 与 getters/setters 属性和标准属性一起使用时需要小心,因为调用略有不同。
例如:
// set
untyped object["set_" + propertyName] = 15;
// get
var value = untyped object["get_" + propertyName];
通常我们对补间库所做的是在 属性 被映射时预先检查它是否是 getter/setter 并存储此信息以供运行时使用。
我希望能够在不使用 Reflect API 的情况下通过名称设置和获取 属性 值。
我正在制作某种补间引擎,为数百个对象设置动画,并且对函数 Reflect.getProperty
和 setProperty
的调用花费了相当多的 CPU 时间。
有没有办法绕过 Reflect API 而不必在我的 Haxe 代码中注入原始 javascript
? (所以在js之外的其他平台也可以使用)
所以不要这样做:
finalValue = Reflect.getProperty(object, propertyName);
我希望能够做到这一点:
finalValue = object[propertyName];
谢谢。
去untyped
怎么样?
// set
untyped object[propertyName] = 15;
// get
var value = untyped object[propertyName];
亲自尝试一下:http://try.haxe.org/#5b61e
Warn Going untyped lets you omit the type system and is more error prone. If you use instances with Haxe properties (getter/setters) it might give unexpected results. That's why using
Reflect
is considered safer.
将 "untyped" 与 getters/setters 属性和标准属性一起使用时需要小心,因为调用略有不同。 例如:
// set
untyped object["set_" + propertyName] = 15;
// get
var value = untyped object["get_" + propertyName];
通常我们对补间库所做的是在 属性 被映射时预先检查它是否是 getter/setter 并存储此信息以供运行时使用。