Prev | Current Page 854 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"


When you use the field() method, it is possible to obtain a variable value or a reference to a function.
var f = Reflect.field(ob, ???execute???);
var a = f(); // the object scope is preserved
var n = Reflect.field(ob, ???name???); // ???name??? is a variable
Reflect.deleteField(ob, ???name???);
trace(Reflect.hasField(ob, ???name???)); // trace ???false???
// this may not work as expected on class instances in Flash 9
Reflect.setField(ob, ???name???, ???John???);
trace(Reflect.hasField(ob, ???name???)); // trace ???true???
The makeVarArgs() method is quite interesting. It transforms a function as shown in the following
example:
var f = function(args : Array < Dynamic > )
{
return args[0] + args[1] + args[2];
}
var fun = Reflect.makeVarArgs(f);
trace(fun(1, 2, 3));
Although the Reflect class is a very powerful tool, it must be used with caution in many situations.
Imagine for example that a variable is defined in this way:
class Person
{
public var name(getName, setName) : String;
//...
}
Outside the Person class, the field is accessed using the identifier name ; but what happens if the
following code is executed?
trace(Reflect.


Pages:
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866