English 中文(简体)
ActionScript:Python的“repr”的等效物(即,对象的有用字符串表示形式)?
原标题:ActionScript: An equivalent of Python s `repr` (ie, a useful string representation of an object)?

Python的repr函数很棒:它返回一个对象的可打印表示。

例如,repr(["a b",{1:2},u"foo"])是字符串 ["a b",{1:2},ufoo] 。请注意,例如,引号被正确地转义。

那么,有没有类似于ActionScript的东西?

例如,现在:[1, 2, ["3", "4"]].toString() 生成的字符串是 "1,2,3,4",这并不是非常有用。我希望它生成类似于 [1, 2, ["3", "4"]] 的字符串。

我考虑使用JSON库...但这不是最理想的,因为它会尝试序列化任意对象的实例,而我不是真正想要的。

问题回答

据我所知,没有任何快捷简单的一行命令来实现您想要的功能,但是这里有一种方法可以直接从Adobe进行操作。

将此翻译成中文:http://livedocs.adobe.com/flex/3/html/help.html?content=usingas_8.html http://livedocs.adobe.com/flex/3/html/help.html?content=usingas_8.html

这是唯一的远程接近的东西:

valueOf ()

公共函数valueOf():对象

Language Version : ActionScript 3.0 Runtime Versions : AIR 1.0, Flash Player 9

返回指定对象的原始值。如果此对象没有原始值,则返回对象本身。

注:目标类别的方法是按目标原型动态生成的。 将这种方法重新定义为反对的子类,并不使用压倒性的关键词。 例如,一组目标执行功能价值Of: 目的不是使用基类的压倒一切。

Returns Object — The primitive value of this object or the object itself.

你可以尝试使用ObjectUtil.toString函数,它不完全符合您的要求,但我认为你不会找到任何更接近你想要的东西了,因为它的功能被描述为“将指定的对象漂亮地打印成一个字符串。”,这正是它所做的,但它会保留更多你不需要的信息。由于Array是一个复杂的数据对象,这就是为什么它会这样注释的原因。

    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 想知道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

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

例如:

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

试试看!

欲了解更多情况,请访问here





相关问题
Disable button tooltip in AS3

I want to disable the tooltip on certain buttons. The tooltip manager seems to be an all or nothing solution. Is it possible to disable the tooltip for just one or two buttons?

Multiple Remote call made simultenously

I was making multiple remote calls and they are done sequentially and when I am getting a result event back it s triggering calls to all the methods with ResultEvent as an argument . I am supposed to ...

Attaching a property to an event in Flex/AS3

I have a parameter that needs to be passed along with an event. After unsuccessful attempts to place it on the type by extending the class, I ve been advised in another SO question to write a custom ...

Clearing RSL in Cache

I have built a flex application which has a "main" project and it is assosciated with a few RSL s which are loaded and cached once i run my "main" application. The problem i am facing is that the ...

What s a good way of deserializing data into mock objects?

I m writing a mock backend service for my flex application. Because I will likely need to add/edit/modify the mock data over time, I d prefer not to generate the data in code like this: var mockData =...

AS3 try/catch out of memory

I m loading a few huge images on my flex/as3 app, but I can t manage to catch the error when the flash player runs out of memory. Here is the what I was thinking might work (I use ???? because i dont ...

热门标签