ActionScript:Python 的“repr”的等价物(即对象的有用字符串表示形式)?
Python 的 repr
函数非常棒:它返回对象的可打印表示形式。
例如,repr(["a'b", {1: 2}, u"foo"])
是字符串 '["a\'b", {1: 2 }, u\'foo\']'
.例如,请注意如何正确转义引号。
那么,ActionScript 有类似的东西吗?
例如,现在: [1, 2, ["3", "4"]].toString()
生成字符串 "1,2,3,4"
...这确实不是很有帮助。我希望它生成一个字符串,例如......嗯,'[1, 2, ["3", "4"]]'
。
我考虑过使用 JSON 库……但这不太理想,因为它会尝试序列化任意对象的实例,而这是我真正不想要的。
Python's repr
function is awesome: it returns a printable representation of an object.
For example, repr(["a'b", {1: 2}, u"foo"])
is the string '["a\'b", {1: 2}, u\'foo\']'
. Notice, eg, how quotes are properly escaped.
So, is there anything like this for ActionScript?
For example, right now: [1, 2, ["3", "4"]].toString()
produces the string "1,2,3,4"
… Which really isn't very helpful. I'd like it to produce a string like… Well, '[1, 2, ["3", "4"]]'
.
I have considered using a JSON library… But that's less than ideal, because it will try to serialize instances of arbitrary objects, which I don't really want.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
AFAIK 没有任何快速简单的一行命令可以完成您想要的操作,但这里有一种方法可以直接从 Adobe 添加
http://livedocs.adobe.com/flex/3/html/help.html?content=usingas_8.html
AFAIK there isn't any quick-easy one line command that does what you want, but here's a way to do it, straight from Adobe I might add
http://livedocs.adobe.com/flex/3/html/help.html?content=usingas_8.html
这是唯一远程关闭的东西:
public function valueOf():Object
Language Version : ActionScript 3.0
运行时版本:AIR 1.0、Flash Player 9
返回指定对象的原始值。如果该对象没有原始值,则返回该对象本身。
注意:Object 类的方法是在 Object 的原型上动态创建的。要在 Object 的子类中重新定义此方法,请勿使用 override 关键字。例如,Object 的子类实现函数 valueOf():Object,而不是使用基类的重写。
退货
对象 — 该对象或对象本身的原始值。
This is the only thing remotley close:
public function valueOf():Object
Language Version : ActionScript 3.0
Runtime Versions : AIR 1.0, Flash Player 9
Returns the primitive value of the specified object. If this object does not have a primitive value, the object itself is returned.
Note: Methods of the Object class are dynamically created on Object's prototype. To redefine this method in a subclass of Object, do not use the override keyword. For example, A subclass of Object implements function valueOf():Object instead of using an override of the base class.
Returns
Object — The primitive value of this object or the object itself.
您可以尝试 ObjectUtil.toString 函数,它并不完全是您想要的,但我认为您不会找到更接近您想要的东西,因为它的函数被描述为“将指定的对象漂亮地打印到字符串中。”,这就是它的作用,但保留了您想要的更多信息。由于数组是一个复杂的数据对象,这就是为什么它这样注释它。
我想知道
repr
如何处理这个例子:You can try the ObjectUtil.toString function, it's not exatly what you want, but I don't think you will find anything closer to what you want as it's functions is described as "Pretty-prints the specified Object into a String.", which is what it does, but keeps much more info that you would want. As Array is a complex data object and that's why it annotates it like that.
I'm wondering how would
repr
handle this example:是的,我知道你想要什么,解决方案很简单,使用
JSON
对象来完成它!例如:
试试吧!
了解更多相关信息,请访问此处。
Yes I know what you want, the solution is quite simple, use
JSON
object to complete it!For example:
Try it!
Read more about that please visit here.