Created
October 27, 2012 18:56
-
-
Save frostney/3965686 to your computer and use it in GitHub Desktop.
jQuery-like extend and each functions for HaXe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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