ActionScript 3 对象的属性名称为字符串?
我想消除在这些中使用魔术字符串:
BindingUtils.bindProperty(obj1, "propertyName", obj2, ["childObj", "anotherProperty"]);
或者
var ddl:DropDownList = new DropDownList();
ddl.labelField = "propertyName";
只输入类似以下内容会很方便:
ddl.labelField = GetPropertyName(ComplexType.propertyName);
它将允许轻松重构,并在属性名称更改时消除运行时错误。 >
有什么想法吗?
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 would eliminate runtime errors when property name changes.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不确定我是否正确理解你的问题。您可以轻松地在单独的类中定义静态常量以消除所有魔术字符串。
Not sure whether I understand your problem correctly. You can easily define static constants in a separate class to eliminate all magic string.
需要“魔法弦”。请记住,这是一种动态语言,凡事都有优点和缺点。这是这些缺点之一。
您可以采取一些措施来限制错误,例如静态属性。
'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.
Stack Overflow 关于类似主题的讨论,其中包含一些您可能感兴趣的想法:
使用对象、字符串、枚举
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