ActionScript 3 object's property name to string?
I want to eliminate usage of magic strings in these:
BindingUtils.bindProperty(obj1, "propertyName", obj2, ["childObj", "anotherProperty"]);
or
var ddl:DropDownList = new DropDownList();
ddl.labelField = "propertyName";
it would be sweet to just type something like:
ddl.labelField = GetPropertyName(ComplexType.propertyName);
It would allow easy refactoring and 开发者_C百科would eliminate runtime errors when property name changes.
Any ideas?
Not sure whether I understand your problem correctly. You can easily define static constants in a separate class to eliminate all magic string.
// In class ConstantContainer public static const PROPERTY_NAME:String = "propertyName"; // In anywhere else ddl.labelField = ConstantContainer.PROPERTY_NAME;
'magic strings' are needed. Remember that this is a dynamic language that has pros and cons to everything. This is one of those cons.
There are a few things you can do to limit error like static properties.
The Stack Overflow discussion on the similar topic with some ideas that can be of interest to you:
Use of object vs string vs enum
精彩评论