Skip to content

Instantly share code, notes, and snippets.

@harold-b
Last active June 20, 2017 20:34
Show Gist options
  • Save harold-b/df24fc5f77e316e2a3c684097256fc8c to your computer and use it in GitHub Desktop.
Save harold-b/df24fc5f77e316e2a3c684097256fc8c to your computer and use it in GitHub Desktop.
Hacky haxe plain javascript object with JS for/in key value loop.
package;
typedef Union2<T1,T2> = haxe.extern.EitherType<T1,T2>;
abstract JSObject( Dynamic ) from Dynamic to Dynamic
{
public inline function new()
{
this = {};
}
@:arrayAccess
public inline function get( key:Union2<String,Int> ):Dynamic
{
return untyped this[key];
}
@:arrayAccess
public inline function set( key:Union2<String,Int>, value:Dynamic )
{
untyped this[key] = value;
}
public inline function forIn( body:String->Void )
{
untyped __js__( "for( var $k in {0} ) {1}", this, body(untyped $k) );
}
public inline function delete( key:Union2<String,Int> )
{
untyped __js__( "delete {0}[{1}]", this, key );
}
}
@harold-b
Copy link
Author

Modified to reflect above method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment