Skip to content

Instantly share code, notes, and snippets.

@frostney
Created October 27, 2012 18:56
Show Gist options
  • Save frostney/3965686 to your computer and use it in GitHub Desktop.
Save frostney/3965686 to your computer and use it in GitHub Desktop.
jQuery-like extend and each functions for HaXe
// Use it like this:
trace(Utils.extend({ 'test': 5, 'one': 1 }, { 'test2': 10 }));
// Output will be:
// {test: 5, one: 1, test2: 10}
Utils.each( { 'one': 1, 'two': 2 }, function(key, value) {
trace(key);
trace(value);
});
// Output will be:
// two
// 2
// one
// 1
package ;
class Utils
{
public static function extend(obj1: Dynamic, obj2: Dynamic): Dynamic {
var keys = Reflect.fields(obj2);
for (k in keys) {
var value: Dynamic = Reflect.field(obj2, k);
Reflect.setField(obj1, k, value);
}
return obj1;
}
public static function each(object: Dynamic, cb: String -> Dynamic -> Void) {
var keys = Reflect.fields(object);
for (k in keys) {
cb(k, Reflect.field(object, k));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment