Trouble Previewing Object as List

I am trying to have a class I am creating be previewed as a list. When you click on a variable with an instance of this class as its value, it will preview it as a list. But when you use it in code, it will still work as intended. I'm pretty sure this is possible because I got it to work using the toString() method.

I tried having the toString() method return a List object. Obviously this wouldn't work though because the code assumed it was a string, and it caused a lot of errors. I have also tried calling the List.toString method but that doesn't cause the preview to be a list, but a string.

Is this a question about Snap!, or about Javascript?

If the latter, you'd have to invent a toList method for your class.

I'm sorry for the confusion. I'm confused because I know how to get the preview for a list:
untitled script pic
I know how to make an object not return "[object] Object":
untitled script pic (2)

But i don't know how to do both at the same time.

The first of those pictures isn't getting you "the preview for a list"; it's getting a list! Lists know how to display themselves, in various formats. But there's no reason to expect other objects to know how to display themselves as pseudo-lists.

If I wanted to do what I think you want to do, I'd write a block that constructs a list of the fields of an instance of your class--an actual list, not a picture of one--and then let Snap! take care of displaying it.

Okay, thank you very much!

I do still have one question to clarify: I'm having the block that creates the object just return a list of the fields, but what if I want to keep some of the properties hidden? Like in by second screen shot, it displays the result as "test", but I can still get the other hidden properties when the value is inputted into another function.

Also, could I also achieve these results by extending the List class?

(I just realized I could have just edited my first reply, I apologize for the spam)

Sure; you'd have to have it extract a sublist for display purposes.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.