Search code examples
javascriptpropertieshaxetween

Haxe Javascript: get and set property by its name without using Reflect


I want to be able to set and get a property value by its name without using the Reflect API. I'm making some sort of a tween engine, animating hundreds of objects and the call to the function Reflect.getProperty and setProperty is taking quite some CPU time.

Is there a way to bypass the Reflect API without having to inject raw javascript inside my Haxe code ? (so it can also be used on other platforms than js)

So instead of doing this:

finalValue = Reflect.getProperty(object, propertyName);

I would like to be able to do this:

finalValue = object[propertyName];

thanks.


Solution

  • What about going untyped?

        // set
        untyped object[propertyName] = 15;
    
        // get
        var value = untyped object[propertyName];
    

    Try it yourself: 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.