ActionScript:Python 的“repr”的等价物(即对象的有用字符串表示形式)?

发布于 2024-08-21 22:21:12 字数 420 浏览 6 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

活泼老夫 2024-08-28 22:21:12

AFAIK 没有任何快速简单的一行命令可以完成您想要的操作,但这里有一种方法可以直接从 Adob​​e 添加

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

哥,最终变帅啦 2024-08-28 22:21:12

这是唯一远程关闭的东西:

valueOf ()

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:

valueOf ()

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.

画离情绘悲伤 2024-08-28 22:21:12

您可以尝试 ObjectUtil.toString 函数,它并不完全是您想要的,但我认为您不会找到更接近您想要的东西,因为它的函数被描述为“将指定的对象漂亮地打印到字符串中。”,这就是它的作用,但保留了您想要的更多信息。由于数组是一个复杂的数据对象,这就是为什么它这样注释它。

    var a:Array = [1, 2, ["3", "4"]];
    trace (ObjectUtil.toString(a));
    // returns
    // (Array)#0
    //  [0] 1
    //  [1] 2
    //  [2] (Array)#1
    //    [0] "3"
    //    [1] "4"

我想知道 repr 如何处理这个例子:

    var a:Array = [0,1,2];
a.push(a);                  
trace (ObjectUtil.toString(a));
    // returns
    // (Array)#0
    //   [0] 0
    //   [1] 1
    //   [2] 2
    //   [3] (Array)#0

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.

    var a:Array = [1, 2, ["3", "4"]];
    trace (ObjectUtil.toString(a));
    // returns
    // (Array)#0
    //  [0] 1
    //  [1] 2
    //  [2] (Array)#1
    //    [0] "3"
    //    [1] "4"

I'm wondering how would repr handle this example:

    var a:Array = [0,1,2];
a.push(a);                  
trace (ObjectUtil.toString(a));
    // returns
    // (Array)#0
    //   [0] 0
    //   [1] 1
    //   [2] 2
    //   [3] (Array)#0
故人爱我别走 2024-08-28 22:21:12

是的,我知道你想要什么,解决方案很简单,使用 JSON 对象来完成它!

例如:

trace(JSON.stringify('hello'));
trace(JSON.stringify(['yet', 'another']));
trace(JSON.stringify({hello: 'world'}));

试试吧!

了解更多相关信息,请访问此处

Yes I know what you want, the solution is quite simple, use JSON object to complete it!

For example:

trace(JSON.stringify('hello'));
trace(JSON.stringify(['yet', 'another']));
trace(JSON.stringify({hello: 'world'}));

Try it!

Read more about that please visit here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文