How do I get names from objects instantiated from a class?

Basicaly, writing up an object with properties and fetching its keys with Object.keys() works fine.

let foo = {
    bar: "abc",
    baz: () => {}
}

// Logs "bar", "baz"
console.log(Object.keys(foo));

However, getting keys from a class instance just yields an empty array

class Foo {
    bar: number;
    baz() {
        // code goes here
    }
}

// Expecting "bar", "baz"
// Output is empty
console.log(Object.keys(new Foo()));

Why is this the case? Is there a way to achieve what I’m trying to do? Please help, I need this for a project. Thanks in advance

3 Likes

I’ve been experimenting with this, as well as sought after help from others and I can’t seem to find a workaround. @richard, is/could this even be possible?

1 Like

@richard @jwunderl I hate being annoying and constantly mentioning people but I’d really like some help, since this seems like an internal issue. I’m currently working on a project and I’ve hit a brick wall due to this. If there’s a way, please let me know, otherwise tell me it’s impossible so I can move on to a different approach in my project.

I really hate spamming mods but this is a very specific issue related to makecode and it seems that I’ve exhausted all resources that could be helpful. Makecode docs also do not mention this kind of issue.

2 Likes

I didn’t see this post, this is one of the differences in our static javascript vs standard javascript listed in https://makecode.com/language#semantic-differences-against-javascript / not yet supported.

1 Like

Oh, sorry about that, my bad. I skimmed through the documentation (that page) but I guess I missed that part. Any plans to support this any time soon?

1 Like